LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


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

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


Hello,
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:
2_23_123
2_24_124
2_22_125
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)
d=$a'_'$b'_'$c
done

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
slinx
Member
 
Registered: Apr 2008
Location: Cleveland, Ohio
Distribution: SuSE, CentOS, Fedora, Ubuntu
Posts: 106

Rep: Reputation: 23
Code:
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
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

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

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
We seem to be doing your homework, eh? You said using bash, so this will do that:
Code:
# 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
nesrin
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
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:16 PM.

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