LinuxQuestions.org
Help answer threads with 0 replies.
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 03-08-2009, 10:03 AM   #1
slcalice
LQ Newbie
 
Registered: Jun 2006
Posts: 5

Rep: Reputation: 0
awk sed shell script


#!/bin/ksh

file='data.txt'
l=1
while read line
do
echo $line
awk '{sub(/^/, "/how could I paste a word in front of a line/");print}' data2.txt > ${line}.txt
((l+=1))
done < "${file}"


I have "data.txt" and "data2.txt" files.

data.txt includes 5 lines like that;

bu
bu1
bu2
bu3
bu4

I want to read each line one by one. For example when I get the first line "bu" I want to put it in front of all the lines in data2.txt file. And I want to get a file which name "bu.txt"

Second I will get "bu1" and put it in data2.txt at the beginning of each line. And I will get "bu2.txt".

...

How could I do that ?

Any suggestion would be welcome.
 
Old 03-08-2009, 07:46 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
some suggestions
1) you do not need to use "sub" or gsub or even awk if you want to use the while loop. to "paste" something in front of a line, just echo it in front . eg echo "new word $line" .
2) to create new files, i am sure you know the > or >> operator. If not, read up the docs (my sig on bash)
 
Old 03-08-2009, 07:56 PM   #3
slcalice
LQ Newbie
 
Registered: Jun 2006
Posts: 5

Original Poster
Rep: Reputation: 0
First of all thanks for the suggestions.

I explained my problem in a detailed way here. Please have a look at it.

http://www.linuxquestions.org/questi...ne-awk-710125/
 
Old 03-09-2009, 05:40 AM   #4
slcalice
LQ Newbie
 
Registered: Jun 2006
Posts: 5

Original Poster
Rep: Reputation: 0
Hi

I solved my problem on my own

awk -v line=$line '{sub(/^/, "/You can paste like this/"line"/");print}' data2.txt > ${line}.txt

here is the power of AWK again

If you want to understand the problem in detail use the link at the previous message.

Thanks to all

this case is closed
 
  


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
Help with BASH script and AWK and SED NickJH Linux - Newbie 11 03-07-2009 04:08 PM
problems with variables using sed or awk in script FeatherKing Programming 4 09-14-2008 11:13 PM
need help with a script that involves awk and sed LinuxLala Programming 2 09-06-2007 09:10 AM
sed and awk in shell script bondoq Linux - Newbie 14 07-27-2007 01:52 AM
ssimple shell script to parse a file ~sed or awk stevie_velvet Programming 7 07-14-2006 03:41 AM

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

All times are GMT -5. The time now is 09:04 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