LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 07-06-2012, 04:21 PM   #1
iconig
LQ Newbie
 
Registered: May 2012
Posts: 28

Rep: Reputation: Disabled
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?
 
Old 07-06-2012, 04:31 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
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
 
Old 07-06-2012, 04:45 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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 04:50 PM. Reason: code fixed (echo)
 
Old 07-06-2012, 05:10 PM   #4
iconig
LQ Newbie
 
Registered: May 2012
Posts: 28

Original Poster
Rep: Reputation: Disabled
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
 
Old 07-06-2012, 05:28 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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 "#"
 
Old 07-06-2012, 05:39 PM   #6
iconig
LQ Newbie
 
Registered: May 2012
Posts: 28

Original Poster
Rep: Reputation: Disabled
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?
 
Old 07-06-2012, 05:52 PM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
In my example the "entry" file is the one containing the entries---you called it "f2"

Please post the code that you tried.
 
Old 07-06-2012, 07:20 PM   #8
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
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.
Old 07-06-2012, 07:52 PM   #9
iconig
LQ Newbie
 
Registered: May 2012
Posts: 28

Original Poster
Rep: Reputation: Disabled
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 07:54 PM.
 
Old 07-06-2012, 07:58 PM   #10
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
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.
 
Old 07-06-2012, 08:04 PM   #11
iconig
LQ Newbie
 
Registered: May 2012
Posts: 28

Original Poster
Rep: Reputation: Disabled
Is the filepath not supposed to do that? Forgive my naivity, trying to learn and get things done at the same time
 
Old 07-06-2012, 08:06 PM   #12
iconig
LQ Newbie
 
Registered: May 2012
Posts: 28

Original Poster
Rep: Reputation: Disabled
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
 
Old 07-06-2012, 08:09 PM   #13
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Quote:
Originally Posted by iconig View Post
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.
 
Old 07-06-2012, 08:12 PM   #14
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Quote:
Originally Posted by iconig View Post
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
 
Old 07-09-2012, 11:59 AM   #15
iconig
LQ Newbie
 
Registered: May 2012
Posts: 28

Original Poster
Rep: Reputation: Disabled
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
 
  


Reply


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
How to check missing header files included from another header file adisan82 Linux - Software 1 01-28-2011 04:57 AM
[exim4] Remove header lines matching received client ip konddor Debian 1 10-23-2009 04:25 AM
Count commas in header and append as many missing from the lines underneath tribal_molecule Programming 1 08-12-2009 07:05 AM
Need help appending text to lines in a file using vi editor tmbrwolf53 Linux - Newbie 3 10-31-2008 04:25 PM
Appending ends of lines in sed Agentrooker Programming 8 08-08-2008 12:19 AM


All times are GMT -5. The time now is 11:43 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration