LinuxQuestions.org
Help answer threads with 0 replies.
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, 03: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, 03:31 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
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, 03:45 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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)
 
Old 07-06-2012, 04: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, 04:28 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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, 04: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, 04:52 PM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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, 06:20 PM   #8
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

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

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
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, 07: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, 07: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, 07:09 PM   #13
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
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, 07:12 PM   #14
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
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, 10: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 03:57 AM
[exim4] Remove header lines matching received client ip konddor Debian 1 10-23-2009 03:25 AM
Count commas in header and append as many missing from the lines underneath tribal_molecule Programming 1 08-12-2009 06:05 AM
Need help appending text to lines in a file using vi editor tmbrwolf53 Linux - Newbie 3 10-31-2008 03:25 PM
Appending ends of lines in sed Agentrooker Programming 8 08-07-2008 11:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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