Bash - While Read Line from File Print First and Second Column

Bash - While read line from file print first and second column

You can use awk like this:

awk -F, '{print "I have", $1, "and the count is", $2}' file

I have apple and the count is 2
I have mango and the count is 5
I have coconut and the count is 10

Though awk is recommended but if you are looking for a bash loop then use:

while IFS=, read -r f c; do
echo "I have $f and the count is $c"
done < file

Print line based on 2nd field value, without using a loop

Given your input file, The below one-liner should work:

awk -F";" '$2 == "b" {print}' myFile.txt

Explanation:

awk -F";"                    ##Field Separator as ";"
'$2 == "b" ##Searches for "b" in the second column($2)
{print}' ##prints the searched line

How do I print 2 columns with looping from a file in bash

What are the problems with your code?

a=$(awk '{ print $1 }' $output)

With echo "a=${a}" you will see, that a is filled with the output for all lines. You were trying to make some find of function, to be called after $a.

while IFS=" " read -r $a $b

Now you are trying to call the "functions" a and b. The code will substitue the value of the variables before reading the inputfile. when a is filled with "Test1 Test2" the code will try to fill the fields $Test1 and $Test2.

When you only want to change the output, without passing the variables to another statement, you can use awk, or

sed -E 's/([^ ]*) ([^ ]*).*/LOG: \1 and \2/' $output
# or
printf 'LOG: %s and %s\n' $(<$output)

In your case, you can make read reading two parameters:

while read -r a b 
do
echo "LOG: $a and $b"
done < "$output"

How to take the first field of each line from a text file and execute it in a while-loop?

If field separator is space, try this:

while read -r line; do
FILE_TO_CHANGE=$(echo $line | awk '{print $1}')
[ -n "$line" ] && chown root "$FILE_TO_CHANGE" && echo -e "$line Ownership changed"
done < "$1"

awk read $line and print first token on standard output, the result is saved in FILE_TO_CHANGE variable and then it is used to run chown.

Another way could be:

awk '{print $1}' $1 | while read line; do
chown root "$line" && echo -e "$line Ownership changed"
done

awk read your file and print the first field of each line, in this case, while loop read awk output line by line and run chown on field.

Extract two Columns from CSV file, split them in while read loop and do different commands with each string

David!

I am a newbie here as well, but I think I have the answer.

If you create a script file with this content and make it executable, you can then type ./script.bash NameOfMyFile.csv and it will do your trick. You can at least use this code as a place to start. Good luck!

#!/bin/bash
file=$1
while IFS=';' read -r newfile contents
do
echo "Create file: $newfile ==> with contents: $contents"
echo $contents > $newfile
done < "$file"

The sample file I fed it looked like this:

Name1; Put this stuff in the first place I want it And then put in more stuff
Name2; I might, have some; puncuated stuff! But it shouldn't matter.
Name3; 20394)@(#$
Name4; No newline at the end of this line

Output:

Create file: Name1  ==> with contents:  Put this stuff in the first place I want it And then put in more stuff
Create file: Name2 ==> with contents: I might, have some; puncuated stuff! But it shouldn't matter.
Create file: Name3 ==> with contents: 20394)@(#$
Create file: Name4 ==> with contents: No newline at the end of this line

I hope this helps!
Kylie

Selecting the column range in bash using while loop

1st solution: In case you want to print only those lines which are satisfying the condition and perform operations only on those then try following.

awk '$1>=2 && $1<=50{$4+=42;print}'  Input_file

Why awk? because it will be faster and is an obvious choice compare to using bash or any shell loop to process a file here.



2nd solution: If OP wants to do only in shell then could try following.

while read first second third fourth
do
if [[ "$first" -ge 2 && "$first" -le 50 ]]
then
echo "$first $second $third $((fourth+42))"
fi
done < "Input_file"

Output will be as follows for above code:

2 B 19 -138
3 C 20 -108
50 D 21 -58

How to get the second column from command output?

Or use sed & regex.

<some_command> | sed 's/^.* \(".*"$\)/\1/'


Related Topics



Leave a reply



Submit