Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
|
07-06-2012, 03:21 PM
|
#1
|
LQ Newbie
Registered: May 2012
Posts: 28
Rep: 
|
appending header between lines
hi, I need to append the same header on each line of a file and creating separate files that contains the header and each line. I know how to append header to a file and saving the new file but I have been trying to do this:
say file named fn1 contains: london 1 1
another file named fn2 contains 23 1 1
34 1 1
5 1 1
I want to get 3 different files named S1, S2 and S3.
S1 will contain: london 1 1
23 1 1
S2 will contain: london 1 1
34 1 1
S3 will contain: london 1 1
5 1 1
Is there a way of doing it?
|
|
|
07-06-2012, 03:31 PM
|
#2
|
LQ Veteran
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
|
Can you post a sample of the original file with the headers: Please wrap it up in the code tags like below:
Code:
To preserve any formatting
And clearly distinguish files S1, S2 and S3:
S1
Code:
Some text wrapped in the code tags
S2
Code:
Some text wrapped in the code tags
S3
Code:
Some text wrapped in the code tags
Thank you
|
|
|
07-06-2012, 03:45 PM
|
#3
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
Partial pseudocode, not tested:
Code:
while read header; do
while read entry; do
<set filename based on # of times thru the loop>
echo $header > $filename
echo $entry >> $filename
done < entryfile
done < headerfile
Last edited by pixellany; 07-06-2012 at 03:50 PM.
Reason: code fixed (echo)
|
|
|
07-06-2012, 04:10 PM
|
#4
|
LQ Newbie
Registered: May 2012
Posts: 28
Original Poster
Rep: 
|
header file name 'f1' : london 1 1
original file name 'f2' and the content is:
23 1 1
34 1 1
5 1 1
Expected result:
filename 's1' contains:
london 1 1
23 1 1
file name 's2' contains:
london 1 1
34 1 1
file name 's3' contains:
london 1 1
5 1 1
Sorry, I could not place it in tags as you said but this is close
|
|
|
07-06-2012, 04:28 PM
|
#5
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
Note: If the "f1" file has only one entry, then do we need it at all? i.e. can you just set a variable to "london 1 1"?
Have you looked at the code I suggested?----any questions?
To put code in code tags, go to the advanced editing mode, select the code, and click on the "#"
|
|
|
07-06-2012, 04:39 PM
|
#6
|
LQ Newbie
Registered: May 2012
Posts: 28
Original Poster
Rep: 
|
taking the header file f1 and each line of f2, then creating separate files with each file containing header and each line is the issue. I tried setting f1 as a variable and running it along with f2 in a loop, but it did not run. what is the entry file?
|
|
|
07-06-2012, 04:52 PM
|
#7
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
In my example the "entry" file is the one containing the entries---you called it "f2"
Please post the code that you tried.
|
|
|
07-06-2012, 06:20 PM
|
#8
|
LQ Veteran
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
|
As pixellany said, if f1 contains only one line it's not really needed, is it?
Try the following:
Code:
#!/bin/bash
f1="london 1 1"
file="/path/to/f2"
a=1
while read line
do
echo $f1 > S$a
echo $line >> S$a
<increment the variable a by 1>
done < $file
It's almost ready. There's only one line missing. You just need to find a way to increment the variable a by one each time it loops.
|
|
1 members found this post helpful.
|
07-06-2012, 06:52 PM
|
#9
|
LQ Newbie
Registered: May 2012
Posts: 28
Original Poster
Rep: 
|
I don't think there should be $file after done since you have already given a file path. I added the increment but it still says syntas error after unexpected token done
#!/bin/bash
f1="london 1 1"
file="/home/cyril/f2"
a=1
while read line do
echo $f1 > $a
echo $line >> $a ; ((a ++ ))
done
Last edited by iconig; 07-06-2012 at 06:54 PM.
|
|
|
07-06-2012, 06:58 PM
|
#10
|
LQ Veteran
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
|
Once again, please use code tags. Click Go Advanced, highlight your code and click #.
Quote:
I don't think there should be $file after done since you have already given a file path.
|
Yes, and how can the while loop know which file to source?
Your increment statement is not correct.
|
|
|
07-06-2012, 07:04 PM
|
#11
|
LQ Newbie
Registered: May 2012
Posts: 28
Original Poster
Rep: 
|
Is the filepath not supposed to do that? Forgive my naivity, trying to learn and get things done at the same time
|
|
|
07-06-2012, 07:06 PM
|
#12
|
LQ Newbie
Registered: May 2012
Posts: 28
Original Poster
Rep: 
|
Code:
Is the filepath not supposed to do that? Forgive my naivity, trying to learn and get things done at the same time
Code:
#!/bin/bash
f1="london 1 1"
file="/home/cyril/f2"
a=1
while read line do
echo $f1 > $a
echo $line >> $a ; ((a ++ ))
done
|
|
|
07-06-2012, 07:09 PM
|
#13
|
LQ Veteran
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
|
Quote:
Originally Posted by iconig
Is the filepath not supposed to do that? Forgive my naivity, trying to learn and get things done at the same time
|
No worries. We all learn something new everyday. The file path is not in any way connected to the while loop in the version that you posted.
|
|
|
07-06-2012, 07:12 PM
|
#14
|
LQ Veteran
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
|
Quote:
Originally Posted by iconig
Code:
Is the filepath not supposed to do that? Forgive my naivity, trying to learn and get things done at the same time
Code:
#!/bin/bash
f1="london 1 1"
file="/home/cyril/f2"
a=1
while read line do
echo $f1 > $a
echo $line >> $a ; ((a ++ ))
done
|
Either use:
Code:
while read line; do
blah
blah
done
or
Code:
while read line
do
blah
blah
done
|
|
|
07-09-2012, 10:59 AM
|
#15
|
LQ Newbie
Registered: May 2012
Posts: 28
Original Poster
Rep: 
|
I adjusted the code as you said and it parsed each line to separate files (wow, that was a major step) but the header file as not included in each file. Here is my code:
Code:
#!/bin/bash
f1="london 1 1"
file="/home/cyril/f2"
let a=1
while read line; do
echo $f2 > S$a
echo $line >> S$a
let a=$(($a+1))
done <f2
|
|
|
All times are GMT -5. The time now is 11:14 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|