String manipulation: extracting a part of a string, text replacement, formatting to a given width, etc.

learn more… | top users | synonyms

1
vote
3answers
14 views

Can't compare lsb_release result to string in Bash

I'm doing something wrong on the string comparison below. It works if I set a variable and compare that, but I can't copy the value to a string. Does anyone know what's wrong? $ if [ "$(lsb_release -...
0
votes
4answers
22 views

Extracting string via grep regex assertions

Assume a text string my_string $ my_string="foo bar=1ab baz=222;" I would like to extract the alphanumeric string between keyword baz and the semi-colon. How do I have to modify the following grep ...
2
votes
3answers
33 views

Consolidate repeated prefix

I have a log file like: Bug123:c:SomeComment Bug222:c:SomeOtherComment Bug123:c:SecondComment I need to produce: Bug123 SomeComment SecondComment Bug222 SomeComment Using bash, I can'...
1
vote
1answer
24 views

Do shells other than Bash and Zsh support ANSI-C quoting? e.g. $'string'

I have a shell script that uses the following to print a green checkmark in its output: col_green="\e[32;01m" col_reset="\e[39;49;00m" echo -e "Done ${col_green}✓${col_reset}" After reading about ...
3
votes
1answer
97 views

bash string concatenation failed

Important note: I'm using Cygwin to run this script. I got a json string by executing result=$(jq -c ".docs[$docIndex] + { \"_rev\": \"rev\" }"<<<"$fileContent") Here is the JSON I get: {"...
0
votes
2answers
32 views

How can I update a unique string in a shell-script with the output of a seperate function?

Due to my laziness, I have written an extremely "messy" series of scripts in order to auto-initiate my openvpn. The configuration file I am using comes vpnbook.com/freevpn. To get the password I use:...
5
votes
3answers
581 views

Test if a string contains a substring

I have the code file="JetConst_reco_allconst_4j2t.png" if [[ $file == *_gen_* ]]; then echo "True" else echo "False" fi I test if file contains "gen". The output is "False". Nice! The ...
2
votes
3answers
69 views

How can I set an environment variable which contains newline characters?

I'm trying to set an RSA key as an environment variable which, as a text file, contains newline characters. Whenever I attempt to read from the file and pass it into an environment variable, it will ...
0
votes
3answers
59 views

How to insert a string into a text variable at specified position

I found many string manipulation tutorials, but can not figure out how to apply them to my particular situation. I need to insert (not substitute) a string variable word into a text variable text ...
3
votes
3answers
687 views

Print month between two words

I am writing some scripts and got stuck with some commands. I would like to put some string (month) into specific place in existing text. Example: Left Right How can I put some text between Left ...
1
vote
4answers
78 views

IF statement to return true if a word contain a specific letter

I need an if statement to return true if a word contain a specific letter. For example: var="information" if [ $var contain "i" ]; then .... else ... fi
1
vote
1answer
27 views

Cannot compare Strings

Google didn't help me. #!/bin/sh j1=`expr "$1"` j2=`expr "$2"` while [ $j1 -le $j2 ]; do date=$(ncal -e $j1) month=$($date | cut -f1 -d' ') if [ $month=="April" ]; then echo $...
1
vote
3answers
31 views

Escaping gawk variables for shell commands

I'm trying to run a small gawk script which will run certain shell commands using system(). I want to get the file list with find and run the script like that: find <arguments> -printf "%f\n" | ...
3
votes
2answers
141 views

How to find a specific file with specific set of strings?

This is the set of files given: ./20170524/18909-20170524182010-PBS74C2VTTCKBMKGQC7YUVEJ3U-362511-19614379.XFA.SOFS_EDI ./20170524/18909-20170524182009-PBS74C2VTTCKBMKGQC7YUVEJ3U-362514-19614381.XFA....
0
votes
2answers
27 views

Bash shell script to remove a guid within a filename

I'm trying to replace a guid without hyphens from some file names. I have the regex done, I think, however I can't seem to get either the escaping correct or the replacement commands to work with ...
0
votes
2answers
20 views

Conducting multi-line string operation with awk instead of for-loop

Assume a multi-line text file (file1) that contains filepaths. $ cat file1 ./foo/foo ./foo/bar ./foo/baz I would like to save only the filenames to file2 and prepend each filename with some special ...
6
votes
2answers
171 views

Bash brace expansion to remove part of filename

Is it possible to remove rather than adding substring to a filename using bash brace expansion? Considering the following scenario, one can add a suffix to a filename by using the below technique: ...
0
votes
1answer
51 views

How to extract part of a string from a filename and sed it into that file

I'm looking to automate a process whereby a part of the filename is what is used to replace a value inside the file itself. Currently I use a manual process that looks like this: get the orgname ...
3
votes
1answer
41 views

How to prefix backslash to output [duplicate]

This is my input file. 3400,2 3400,27 3400,DC 3400,TF 5600,KA 5600,KN 5600,TF 0313,EX 0313,EY 0313,EZ 0313,FD 0313,FE 0313,JB 0313,JC 0313,KG 0313,T8 I want my output to be 400,2\|3400,27\|3400,DC\|...
0
votes
2answers
59 views

How to find 4th and 5th character of files name?

$ ls abcmkde ghemkde vdecdde For example I only want to list out file name with only 4 and 5th character with only one iteration of single match (mk and cd in the example above). There is a bunch of ...
3
votes
2answers
49 views

Extracting number from filename

I have a filename following this model: 1.raw_bank_details_211.trg 2.raw_bank_details_222.trg I need to use the cut command in unix and cut the above string to obtain 211 and 222 from the strings ...
11
votes
3answers
892 views

Does bash support back references in parameter expansion?

I have a variable named descr which can contain a string Blah: -> r1-ae0-2 / [123], -> s7-Gi0-0-1:1-US / Foo, etc. I want to get the -> r1-ae0-2, -> s7-Gi0-0-1:1-US part from the string. ...
0
votes
3answers
39 views

If file data starts with a certain hex sequence, run strings command on the file

I want to recursively crawl a directory and if the file has \x58\x46\x53\x00 as the first 4 bytes, I want to run strings on the file.
1
vote
1answer
381 views

How to run string with values as a command in bash?

Here is my small bash script snippet. i=5 command='echo $i' $command I want this script to print 5 i.e., I want it to run echo and print 5. But it instead keeps printing $i. So how do I go about ...
0
votes
1answer
134 views

Match a string with a substring in Expect scripting

I have an output like this: Node Name Status IP Address ====================================================== bw2acn0 ENABLED x.x.x.x bw2acn1 ...
0
votes
1answer
54 views

Bash variable substitution with spaces [duplicate]

There's next code: TITLE="Some value with spaces" DIALOG="${DIALOG=dialog} --clear --no-tags --tab-correct --backtitle $TITLE " ... $DIALOG --title "Some title --menu "Menu" 15 60 5 "1" "menu1" "2" "...
2
votes
2answers
32 views

Combining two variables mangles them on Windows

I'm trying to write something for Bash on the Windows System for Linux that converts ~ to your Windows user directory when passed through a winpath function. So far, I'm able to retrieve the Windows ...
0
votes
4answers
90 views

Get specific word and following text

Input File Mar 21 13:25:04 ip-172-2-0-53 sendmail[5857]: v2LKMUDq005855: to=<[email protected]>,<[email protected]>, delay=00:02:34, xdelay=00:02:34, mailer=esmtp, pri=151745, relay=...
1
vote
3answers
52 views

How to print each line of a file as many times as according to the number in the first column

Input_file: 1 string1 4 string2 2 string3 ... Output_file: 1 string1 4 string2 4 string2 4 string2 4 string2 2 string3 2 string3 ... Here's my code and it didn't work :( Please help! #!/bin/bash N=...
0
votes
1answer
62 views

Splitting of bash variable

We have cgi application written in bash scripts. I am trying to understand how the code snippet below works. Basically its a dropdown menu and the choices is governed by _getDocType logic. How does ...
-1
votes
1answer
63 views

Argument list is too long for /bin/aws

I am working on a script that would loop through a text file set up as such: snap-foo bar 20170202 This text file basically has Snapshot IDs, a tag we use to identify the instances associated,...
0
votes
1answer
37 views

how to echo some unique character [duplicate]

I want to echo some unique string to a file. Sample code like bellow: { echo " LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined" echo " LogFormat "%h %l %u %t \"...
3
votes
3answers
120 views

Number of new lines in a string as integer [duplicate]

I want to find the number of new lines in a string and then use this number in a while loop as the limit. I can find and write the number of lines using: echo "$MY_STRING" | grep -c '$' In order to ...
3
votes
4answers
62 views

Variable output doesn't concatenate

I have a script that concatenates output from two different variables. The issue is that the output of both variables contains multiple rows. So, the output isn't what I expect. First variable and ...
6
votes
9answers
2k views

How do I parse out just the date from 2017-03-08T19:41:26Z?

I am trying to parse out just the date from 2017-03-08T19:41:26Z. The desired output is 2017-03-08.
5
votes
4answers
250 views

Extracting substring from environment variable

In a bash or zsh script, how might I extract the host from a url, e.g. unix.stackexchange.com from http://unix.stackexchange.com/questions/ask, if the latter is in an environment variable?
-1
votes
2answers
82 views

How to avoid a variable containing a list of directories being treated as string [closed]

#!/bin/bash Dir='/home/TEST' Project="${Dir}/Project.txt" The text file Project.txt contains the file type & its path: SOURCE|FILE|TARGET_PATH TEST|BDL|/LOAN/NDL/XML TEST|BDL|/LOAN/DL/XML TEST|...
3
votes
2answers
76 views

add subdirectories to $PATH in bash using find

I tried this PATH=$PATH$( find $HOME/scripts/ -type d -printf ":%p" ) but it works only on Linux, on OSX (or Freebsd), it's not working because -printf is not POSIX. How can I write a compatible ...
0
votes
2answers
52 views

How to grep the all filenames in folderA and (folderB inside folderA)?

I wanted to list down all the filenames within a folderA and another folderB inside folderA and saved it to a text file. The code I used is: ls -1 /home/CSV_XXX/* | grep ^'/home/CSV_XXX' >/home/...
0
votes
0answers
17 views

polyphase decomposition of a string [duplicate]

How can one decompose a string in N phases using bash? Example string: "abcdefghijklm" N = 2 > phase 0: "acegikm", phase 1: "bdfhjl" N = 3 > phase 0: "adgjm", phase 1: "behk", phase 2: "cfil"
0
votes
3answers
256 views

sed to replace matching word to end of line

I am trying to remove all characters after particular match pattern i.e nfin to the end of line. sample_text xxsac00mi126 vddai:f1042 tkeept_iph xsac00.f1040 vdda lvtpfet m=1 nf=1 nfin=2 pdej=1.6e-...
0
votes
2answers
72 views

Printing ranges of output with echo

I found code for getting the current Caps Lock status (since this laptop's keyboard doesn't have any LED indicators for it) from here. #!/bin/bash v=`xset -q | grep Caps` echo ${v:7:17} I ...
3
votes
1answer
58 views

IF ELSE string comparison from curl

When I do: URL_CURL_CHECK="https://github.com/" VAR_A=$(curl -Is --head "$URL_CURL_CHECK" | grep "HTTP/1.1 200 OK") VAR_B="HTTP/1.1 200 OK" echo if [ "$VAR_A" != "$VAR_B" ]; then echo "Not equal ...
1
vote
3answers
49 views

Using bash to reformat “#include” in a list of files using regex

Consider the following code that does a simple loop over code files: #!/bin/bash dir="." find $dir -name *.cpp -o -name *.h | while read file; do echo "processing: "$file # Process file here ...
-1
votes
1answer
82 views

String comparison not working in bash

Simple string comparison is not working in bash. Can someone please help what's wrong with this : $ cat compare.sh function compare { BEFORE_STATUS=enabled AFTER_STATUS=disabled if [ $BEFORE_STATUS ...
0
votes
1answer
393 views

Jenkins pipelines, sh, quotes and spaces

I am trying to setup sh command as part of my Jenkins pipeline. This command has one parameter where list of values is supplied. Those values have spaces in them. I have tried so many things, arrays, ...
0
votes
1answer
35 views

How to give IFS 2 chars to split based on both not separately in shell scripting?

I use the below bash script to split string: string='/dev/shm: 0%used(0MB/4003MB) /var/spool/site/storage/users: 64%used(64437MB/100760MB) /run/user/1001: 0%used(0MB/801MB) /run: 10%used(165MB/1601MB)...
0
votes
1answer
34 views

Search For Different File Name in Loop

I want to do search through a bunch of directories to find missing files. The format of the directories and files is YYYY/MM/file_name_YYYYMMDD.csv. So what I want to do is something like: for date ...
3
votes
1answer
35 views

Add string from file to filename

I need to rename files to include a string found in the first line. ex: [acoder@test]# head -1 FILE001.DAT XYZ123 Here's my best guess: for file in /some/dir/*.DAT do CODE=`head -1 "$...
1
vote
2answers
336 views

How to do a string comparison ignore whitespace?

How would you compare two strings but ignoring whitespaces? I am doing the following: if [ "$a" == "$b" ]; then echo ok fi But it doesn't seem to match. I've printed what "a" and "b" are and they ...