How to read two files in a nested loop in Unix - linux

My objective here is to read each revision present in revisions.txt and extract the text against that revision number from logs.txt file.
For example revisions.txt file has the following inputs,
APP-1001
APP-1002
APP-1004
And logs.txt file has the following inputs,
APP-999 : Bug Fix for XYZ issues
APP-1001 : Bug Fix for XYZ issues
APP-1002 : Bug Fix for XYZ issues
APP-1003 : Bug Fix for XYZ issues
APP-1004 : Bug Fix for XYZ issues
I want to get all the lines against all revisions in the revisions.txt file. I came up with the following code,
#!/bin/bash
echo "Start!"
input=logs.txt
revision=revisions.txt
IFS=$'\n' read -d '' -r -a revs < $revision
for rev in "${revs[#]}"
do
echo $rev
while IFS='' read -r line || [[ -n "$line" ]]; do
regex='^'$rev'(.*)'
if [[ $line =~ $regex ]];
then
echo $line
fi
done < "$input"
done
Output as of now,
APP-1001
APP-1002
APP-1004
APP-1004 : Bug Fix for XYZ issues
It's only printing text for the last revision i.e. APP-1004. I read on the web that it's not possible to read two files in nested loop.
Please suggest a different way of doing this.
Thanks in advance.

You can nest the loops and hence, you can iterate from both files at the same time.
while read revisions_line
do
while read logs_line
do
echo "This line comes from revisions : $revisions_line"
echo "This line comes from logs : $logs_line"
done << "$( cat logs.txt)"
done << "$( cat revisions.txt)"
Regards!

You can simply use below script for the same:-
#!/bin/bash
while read line
do
#echo "$line logs.txt"
log_info=$(grep $line logs.txt)
if [ log_info != "" ]; then
echo "$log_info" | awk '{$1=$2=""; print $0}' | sed 's/^[ \t]*//'
fi
done < revisions.txt
How will it work? While loop will read each line from file revisions.txt and grep will search for that in logs.txt file and if found it will store that line in variable log_info. Now if log_info variable is not empty the line will be redirect to awk command and awk command will remove column1 and column2 from the line and redirect the output to sed command and finally sed will remove the leading blank space from it. The final out put will be:-
Bug Fix for XYZ issues
Bug Fix for XYZ issues
Bug Fix for XYZ issues
If you want the whole string just modify like below:-
echo "$log_info"
If you don't bother about the leading blank space you can go ahead with below:-
echo "$log_info" | awk '{$1=$2=""; print $0}'

Related

How to use line that read from file in grep command

I'm sorry for my poor English, first.
I want to read a file (tel.txt) that contains many tel numbers (a number per line) and use that line to grep command to search about the specific number in the source file (another file)!
I wrote this code :
dir="/home/mujan/Desktop/data/ADSL_CDR_Text_Parts_A"
file="$dir/tel.txt"
datafile="$dir/ADSL_CDR_Like_Tct4_From_960501_to_97501_Part0.txt"
while IFS= read -r line
do
current="$line"
echo `grep -F $current "$datafile" >> output.txt`
done < $file
the tel file sample :
44001547
44001478
55421487
but that code returns nothing!
when I declare 'current' variable with literals it works correctly!
what happened?!
Your grep command is redirected to write its output to a file, so you don't see it on the terminal.
Anyway, you should probably be using the much simpler and faster
grep -Ff "$file" "$datafile"
Add | tee -a output.txt if you want to save the output to a file and see it at the same time.
echo `command` is a buggy and inefficient way to write command. (echo "`command`" would merely be inefficient.) There is no reason to capture standard output into a string just so that you can echo that string to standard output.
Why don't you search for the line var directly? I've done some tests, this script works on my linux (CentOS 7.x) with bash shell:
#!/bin/bash
file="/home/mujan/Desktop/data/ADSL_CDR_Text_Parts_A/tel.txt"
while IFS= read -r line
do
echo `grep "$line" /home/mujan/Desktop/data/ADSL_CDR_Text_Parts_A/ADSL_CDR_Like_Tct4_From_960501_to_97501_Part0.tx >> output.txt`
done < $file
Give it a try... It shows nothing on the screen since you're redirecting the output to the file output.txt so the matching results are saved there.
You should use file descriptors when reading with while loop.instead use for loop to avoid false re-directions
dir="/home/mujan/Desktop/data/ADSL_CDR_Text_Parts_A"
file="$dir/tel.txt"
datafile="$dir/ADSL_CDR_Like_Tct4_From_960501_to_97501_Part0.txt"
for line in `cat $file`
do
current="$line"
echo `grep -F $current "$datafile" >> output.txt`
done

Linux shell script, use variable more than once

I've created a script that accesses a website with the use of a datafile and outputs the sites responses (one line XML) to an output file. I would like the output to start with the query of the datafile and then the response of the website. When I echo the query one one line and write it to an output file and then write the site's response to the same output file it uses two lines but I only want one line because I would like to end up with a comma separated file that I can import in excel.
This works but with having two lines of data:
while read -r line || [[ -n $line ]]
do
datatogather="$line"
echo $datatogather >>outputfile.txt
curl http://login:password#somewebsite.info/application.php?$datatogather >>outputfile.txt
echo >>outputfile.txt
done < datafile.txt
This doesn't work (although it shows the comma in the output file, so that line is being processed):
while read -r line || [[ -n $line ]]
do
datatogather="$line"
echo $datatogather,>>outputfile.txt | curl http://login:password#somewebsite.info/application.php?$datatogather >>outputfile.txt
echo >>outputfile.txt
done < datafile.txt
Stripping the output file of it's garbage data with sed was a breeze to figure out, even reading the input file into the site was very easy compared to figuring out how to use a variable more than once in a single line. Hope you can help me.
Do one echo operation instead of two, and only one redirection instead of many, and use command substitution to capture the output of curl:
while read -r line && [[ -n "$line" ]]
do
echo "$line,$(curl http://login:password#example.com/application.php?$line)"
done < datafile.txt >>outputfile.txt
Note that the test is changed from || to &&, and personally I don't like unquoted variables, though [[ is less problematic in some respects than [ (but introduces other problems, IMO).
The only nasty feature there is that the double quotes mean that newlines in the website response are preserved in the output. If you like living dangerously, you could simply remove the double quotes. It would probably be better to revise it to map the newlines to spaces:
while read -r line && [[ -n "$line" ]]
do
echo "$line,$(curl http://login:password#example.com/application.php?$line | tr '\n' ' ')"
done < datafile.txt >>outputfile.txt
Note that you're liable to have problems if the output from the website includes any commas in its output, but you've not (yet) asked about that.
You do not want to put the tr operation outside the loop; you want a newline at the end of each echo, so this would be bad:
while read -r line && [[ -n "$line" ]]
do
echo "$line,$(curl http://login:password#example.com/application.php?$line)"
done < datafile.txt | tr '\n' ' ' >>outputfile.txt

Find all instances of word occurring in a file

Using bash I was wondering how I can find all instances of a word which starts with text, store it as a variable and print it out.
For example if this was my file
test.conf
$temp_test
test
$1234_$temp
$temp_234
My output would be as follows:
$temp_test
$temp_234
Can anyone tell me how this might be possible? This is the closest I could get so far.
while read NAME
do
echo "$NAME"
done < test.conf
You can just use grep with right regex:
grep '^ *$temp' test.conf
$temp_test
$temp_234
UPDATE: As per comments:
while read -r l; do
echo "$l"
done < <(sed -n '/^ *$temp_/s/^ *\$temp_//p' t.conf)
test
234

Why "read" does not read the first line of a command output?

I want to read the first line of a command output with. but I get an empty message.
I used
ls | read line
echo $line #nothing displayed
Why the line variable is empty and how to fix that?
The following code works. but I want to read only the first line
ls | while read line; do
echo $line
done
If it's not possible with read, is it possible to do it with other functions like grep, awk, sed ?
The read does read the first line, but it is being executed in a subshell. But you can do:
ls | { read line;
echo $line;
# other commands using $line;
}
# After the braces, $line is whatever it was before the braces.
You can use
read line <<< "$(ls)"
printf "%s\n" "$line"
But as already mentioned, please, PLEASE, PLEASE do not parse ls output!

Looping through the content of a file in Bash

How do I iterate through each line of a text file with Bash?
With this script:
echo "Start!"
for p in (peptides.txt)
do
echo "${p}"
done
I get this output on the screen:
Start!
./runPep.sh: line 3: syntax error near unexpected token `('
./runPep.sh: line 3: `for p in (peptides.txt)'
(Later I want to do something more complicated with $p than just output to the screen.)
The environment variable SHELL is (from env):
SHELL=/bin/bash
/bin/bash --version output:
GNU bash, version 3.1.17(1)-release (x86_64-suse-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.
cat /proc/version output:
Linux version 2.6.18.2-34-default (geeko#buildhost) (gcc version 4.1.2 20061115 (prerelease) (SUSE Linux)) #1 SMP Mon Nov 27 11:46:27 UTC 2006
The file peptides.txt contains:
RKEKNVQ
IPKKLLQK
QYFHQLEKMNVK
IPKKLLQK
GDLSTALEVAIDCYEK
QYFHQLEKMNVKIPENIYR
RKEKNVQ
VLAKHGKLQDAIN
ILGFMK
LEDVALQILL
One way to do it is:
while read p; do
echo "$p"
done <peptides.txt
As pointed out in the comments, this has the side effects of trimming leading whitespace, interpreting backslash sequences, and skipping the last line if it's missing a terminating linefeed. If these are concerns, you can do:
while IFS="" read -r p || [ -n "$p" ]
do
printf '%s\n' "$p"
done < peptides.txt
Exceptionally, if the loop body may read from standard input, you can open the file using a different file descriptor:
while read -u 10 p; do
...
done 10<peptides.txt
Here, 10 is just an arbitrary number (different from 0, 1, 2).
cat peptides.txt | while read line
do
# do something with $line here
done
and the one-liner variant:
cat peptides.txt | while read line; do something_with_$line_here; done
These options will skip the last line of the file if there is no trailing line feed.
You can avoid this by the following:
cat peptides.txt | while read line || [[ -n $line ]];
do
# do something with $line here
done
Option 1a: While loop: Single line at a time: Input redirection
#!/bin/bash
filename='peptides.txt'
echo Start
while read p; do
echo "$p"
done < "$filename"
Option 1b: While loop: Single line at a time:
Open the file, read from a file descriptor (in this case file descriptor #4).
#!/bin/bash
filename='peptides.txt'
exec 4<"$filename"
echo Start
while read -u4 p ; do
echo "$p"
done
This is no better than other answers, but is one more way to get the job done in a file without spaces (see comments). I find that I often need one-liners to dig through lists in text files without the extra step of using separate script files.
for word in $(cat peptides.txt); do echo $word; done
This format allows me to put it all in one command-line. Change the "echo $word" portion to whatever you want and you can issue multiple commands separated by semicolons. The following example uses the file's contents as arguments into two other scripts you may have written.
for word in $(cat peptides.txt); do cmd_a.sh $word; cmd_b.py $word; done
Or if you intend to use this like a stream editor (learn sed) you can dump the output to another file as follows.
for word in $(cat peptides.txt); do cmd_a.sh $word; cmd_b.py $word; done > outfile.txt
I've used these as written above because I have used text files where I've created them with one word per line. (See comments) If you have spaces that you don't want splitting your words/lines, it gets a little uglier, but the same command still works as follows:
OLDIFS=$IFS; IFS=$'\n'; for line in $(cat peptides.txt); do cmd_a.sh $line; cmd_b.py $line; done > outfile.txt; IFS=$OLDIFS
This just tells the shell to split on newlines only, not spaces, then returns the environment back to what it was previously. At this point, you may want to consider putting it all into a shell script rather than squeezing it all into a single line, though.
Best of luck!
A few more things not covered by other answers:
Reading from a delimited file
# ':' is the delimiter here, and there are three fields on each line in the file
# IFS set below is restricted to the context of `read`, it doesn't affect any other code
while IFS=: read -r field1 field2 field3; do
# process the fields
# if the line has less than three fields, the missing fields will be set to an empty string
# if the line has more than three fields, `field3` will get all the values, including the third field plus the delimiter(s)
done < input.txt
Reading from the output of another command, using process substitution
while read -r line; do
# process the line
done < <(command ...)
This approach is better than command ... | while read -r line; do ... because the while loop here runs in the current shell rather than a subshell as in the case of the latter. See the related post A variable modified inside a while loop is not remembered.
Reading from a null delimited input, for example find ... -print0
while read -r -d '' line; do
# logic
# use a second 'read ... <<< "$line"' if we need to tokenize the line
done < <(find /path/to/dir -print0)
Related read: BashFAQ/020 - How can I find and safely handle file names containing newlines, spaces or both?
Reading from more than one file at a time
while read -u 3 -r line1 && read -u 4 -r line2; do
# process the lines
# note that the loop will end when we reach EOF on either of the files, because of the `&&`
done 3< input1.txt 4< input2.txt
Based on #chepner's answer here:
-u is a bash extension. For POSIX compatibility, each call would look something like read -r X <&3.
Reading a whole file into an array (Bash versions earlier to 4)
while read -r line; do
my_array+=("$line")
done < my_file
If the file ends with an incomplete line (newline missing at the end), then:
while read -r line || [[ $line ]]; do
my_array+=("$line")
done < my_file
Reading a whole file into an array (Bash versions 4x and later)
readarray -t my_array < my_file
or
mapfile -t my_array < my_file
And then
for line in "${my_array[#]}"; do
# process the lines
done
More about the shell builtins read and readarray commands - GNU
More about IFS - Wikipedia
BashFAQ/001 - How can I read a file (data stream, variable) line-by-line (and/or field-by-field)?
Related posts:
Creating an array from a text file in Bash
What is the difference between thee approaches to reading a file that has just one line?
Bash while read loop extremely slow compared to cat, why?
Use a while loop, like this:
while IFS= read -r line; do
echo "$line"
done <file
Notes:
If you don't set the IFS properly, you will lose indentation.
You should almost always use the -r option with read.
Don't read lines with for
If you don't want your read to be broken by newline character, use -
#!/bin/bash
while IFS='' read -r line || [[ -n "$line" ]]; do
echo "$line"
done < "$1"
Then run the script with file name as parameter.
Suppose you have this file:
$ cat /tmp/test.txt
Line 1
Line 2 has leading space
Line 3 followed by blank line
Line 5 (follows a blank line) and has trailing space
Line 6 has no ending CR
There are four elements that will alter the meaning of the file output read by many Bash solutions:
The blank line 4;
Leading or trailing spaces on two lines;
Maintaining the meaning of individual lines (i.e., each line is a record);
The line 6 not terminated with a CR.
If you want the text file line by line including blank lines and terminating lines without CR, you must use a while loop and you must have an alternate test for the final line.
Here are the methods that may change the file (in comparison to what cat returns):
1) Lose the last line and leading and trailing spaces:
$ while read -r p; do printf "%s\n" "'$p'"; done </tmp/test.txt
'Line 1'
'Line 2 has leading space'
'Line 3 followed by blank line'
''
'Line 5 (follows a blank line) and has trailing space'
(If you do while IFS= read -r p; do printf "%s\n" "'$p'"; done </tmp/test.txt instead, you preserve the leading and trailing spaces but still lose the last line if it is not terminated with CR)
2) Using process substitution with cat will reads the entire file in one gulp and loses the meaning of individual lines:
$ for p in "$(cat /tmp/test.txt)"; do printf "%s\n" "'$p'"; done
'Line 1
Line 2 has leading space
Line 3 followed by blank line
Line 5 (follows a blank line) and has trailing space
Line 6 has no ending CR'
(If you remove the " from $(cat /tmp/test.txt) you read the file word by word rather than one gulp. Also probably not what is intended...)
The most robust and simplest way to read a file line-by-line and preserve all spacing is:
$ while IFS= read -r line || [[ -n $line ]]; do printf "'%s'\n" "$line"; done </tmp/test.txt
'Line 1'
' Line 2 has leading space'
'Line 3 followed by blank line'
''
'Line 5 (follows a blank line) and has trailing space '
'Line 6 has no ending CR'
If you want to strip leading and trading spaces, remove the IFS= part:
$ while read -r line || [[ -n $line ]]; do printf "'%s'\n" "$line"; done </tmp/test.txt
'Line 1'
'Line 2 has leading space'
'Line 3 followed by blank line'
''
'Line 5 (follows a blank line) and has trailing space'
'Line 6 has no ending CR'
(A text file without a terminating \n, while fairly common, is considered broken under POSIX. If you can count on the trailing \n you do not need || [[ -n $line ]] in the while loop.)
More at the BASH FAQ
I like to use xargs instead of while. xargs is powerful and command line friendly
cat peptides.txt | xargs -I % sh -c "echo %"
With xargs, you can also add verbosity with -t and validation with -p
This might be the simplest answer and maybe it don't work in all cases, but it is working great for me:
while read line;do echo "$line";done<peptides.txt
if you need to enclose in parenthesis for spaces:
while read line;do echo \"$line\";done<peptides.txt
Ahhh this is pretty much the same as the answer that got upvoted most, but its all on one line.
#!/bin/bash
#
# Change the file name from "test" to desired input file
# (The comments in bash are prefixed with #'s)
for x in $(cat test.txt)
do
echo $x
done
Here is my real life example how to loop lines of another program output, check for substrings, drop double quotes from variable, use that variable outside of the loop. I guess quite many is asking these questions sooner or later.
##Parse FPS from first video stream, drop quotes from fps variable
## streams.stream.0.codec_type="video"
## streams.stream.0.r_frame_rate="24000/1001"
## streams.stream.0.avg_frame_rate="24000/1001"
FPS=unknown
while read -r line; do
if [[ $FPS == "unknown" ]] && [[ $line == *".codec_type=\"video\""* ]]; then
echo ParseFPS $line
FPS=parse
fi
if [[ $FPS == "parse" ]] && [[ $line == *".r_frame_rate="* ]]; then
echo ParseFPS $line
FPS=${line##*=}
FPS="${FPS%\"}"
FPS="${FPS#\"}"
fi
done <<< "$(ffprobe -v quiet -print_format flat -show_format -show_streams -i "$input")"
if [ "$FPS" == "unknown" ] || [ "$FPS" == "parse" ]; then
echo ParseFPS Unknown frame rate
fi
echo Found $FPS
Declare variable outside of the loop, set value and use it outside of loop requires done <<< "$(...)" syntax. Application need to be run within a context of current console. Quotes around the command keeps newlines of output stream.
Loop match for substrings then reads name=value pair, splits right-side part of last = character, drops first quote, drops last quote, we have a clean value to be used elsewhere.
This is coming rather very late, but with the thought that it may help someone, i am adding the answer. Also this may not be the best way. head command can be used with -n argument to read n lines from start of file and likewise tail command can be used to read from bottom. Now, to fetch nth line from file, we head n lines, pipe the data to tail only 1 line from the piped data.
TOTAL_LINES=`wc -l $USER_FILE | cut -d " " -f1 `
echo $TOTAL_LINES # To validate total lines in the file
for (( i=1 ; i <= $TOTAL_LINES; i++ ))
do
LINE=`head -n$i $USER_FILE | tail -n1`
echo $LINE
done
#Peter: This could work out for you-
echo "Start!";for p in $(cat ./pep); do
echo $p
done
This would return the output-
Start!
RKEKNVQ
IPKKLLQK
QYFHQLEKMNVK
IPKKLLQK
GDLSTALEVAIDCYEK
QYFHQLEKMNVKIPENIYR
RKEKNVQ
VLAKHGKLQDAIN
ILGFMK
LEDVALQILL
Another way to go about using xargs
<file_name | xargs -I {} echo {}
echo can be replaced with other commands or piped further.
for p in `cat peptides.txt`
do
echo "${p}"
done

Resources