Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-29-2010, 08:33 AM   #1
LQ Newbie
Registered: Nov 2010
Posts: 15

Rep: Reputation: 0
line by line reading and writing in Bash

I have a text file (myfile.txt) with columns a,b,c.
123 2 23
124 2 24
125 2 22
I want to read this file line by line and for each line i want to add a 4th column (b_c_a) which will be:
I tried "while read line" in bash script but i could not get what i wanted.

while read line; do
a=$(awk'{print $1}' myfile.txt)
b=$(awk'{print $2}' myfile.txt)
c=$(awk'{print $3}' myfile.txt)

this gives:
2 2 2_23 24 25_123 124 125

I would appreciate if somebody could help me.
thanks in advance.
Old 11-29-2010, 08:51 AM   #2
Registered: Apr 2008
Location: Cleveland, Ohio
Distribution: SuSE, CentOS, Fedora, Ubuntu
Posts: 106

Rep: Reputation: 23
awk '{printf "%s\t%s\t%s\t%s\n", $1, $2, $3, $3 "_" $2 "_" $1 }' file.txt

Last edited by slinx; 11-29-2010 at 08:51 AM. Reason: typo
1 members found this post helpful.
Old 11-29-2010, 08:52 AM   #3
LQ Veteran
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389

Is this what you are looking for:
awk '{ print $0, $2"_"$3"_"$1 }' infile
Hope this helps.
1 members found this post helpful.
Old 11-29-2010, 11:26 AM   #4
Amigo developer
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,856

Rep: Reputation: 547Reputation: 547Reputation: 547Reputation: 547Reputation: 547Reputation: 547
We seem to be doing your homework, eh? You said using bash, so this will do that:
# initilaize an empty output file
> output-file-name
# maybe 'cat /dev/null output-file-name' is easier to understand

while read a b c ; do
 echo $a $b $c $b_$c_$a >> output-file-name
done <input-file-name
1 members found this post helpful.
Old 12-01-2010, 08:19 AM   #5
LQ Newbie
Registered: Nov 2010
Posts: 15

Original Poster
Rep: Reputation: 0
thanks for the help,
all three solutions are working very well.

only on this line there is a little correction:
echo $a $b $c $0b"_"$c"_"$a >> output-file-name

PS: It is not a homework, i dont even have to do this in shell. but i would like to learn a bit how shell scripting works


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
reading variable while remaining on the same line - bash GSMD Programming 3 04-24-2011 09:47 AM
Programing Question: Reading file line by line.. then char by char in each line adriumroot Programming 8 05-30-2010 01:55 AM
Reading screen output line by line basildon Linux - Newbie 1 09-23-2008 01:33 PM
reading and writing random line from file mcbenus Linux - Software 6 06-30-2008 09:39 PM
bash: reading a particular line seb3579 Linux - Newbie 2 09-14-2004 03:01 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:59 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration