LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 11-27-2012, 02:29 PM   #1
atjurhs
Member
 
Registered: Aug 2012
Posts: 190

Rep: Reputation: Disabled
adding a header from another file


Hi guys,

today I got stuck with a new file format, and I need to give it both a header line (all my other tools want a header line) and "space,space" between the columns that are curently just space seperated.

I figured out that I could do
Code:
#!/bin/bash
sed -i '/\ /s/\ / , /g' inputfile.dat
sed -i '1 \A , B , C , D' inputfile.dat
and that will work except typing "A , B , C , D , etc....." is a big pain to input the header strings to sed.

so, I wonder if I have a seperate called HeaderFile.txt or something like that that has each of the header titles, can sed read that and add it to the columns?

in psuedo-code something like
Code:
sed -i '1 \HeaderFile.txt' inputfile.dat
the HeaderFile.txt file might look someting like
Code:
string1 , string2 , string3 , string4 , etc....
or someting like
Code:
string1
string2
string3
string4
etc...."
whatever is easier

thnks so much for whatever help you can give,

Tabitha
 
Old 11-27-2012, 03:01 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Two solutions using sed. A pure sed code to insert text from a file before the first line:
Code:
sed -i '1 {
  r header
  h
  d
}
2 {
  H
  g
}' file
and a mixed sed/bash solution:
Code:
sed -i "1s/^/$(cat header)\n/" file
where header is the name of the file containing the header in a single line. Hope this helps.
 
Old 11-27-2012, 03:29 PM   #3
atjurhs
Member
 
Registered: Aug 2012
Posts: 190

Original Poster
Rep: Reputation: Disabled
colucix

well I've learned a lot of awk/sed/bash over the past several months, and I was playing with cat but didn't know how to use it

I tried your script and it worked great!

thanks soooo much

Tabby
 
Old 11-27-2012, 03:36 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
You're welcome!
 
Old 12-12-2012, 01:42 PM   #5
atjurhs
Member
 
Registered: Aug 2012
Posts: 190

Original Poster
Rep: Reputation: Disabled
ok, well now I got a file with all the headers listed by rows, and I need to cat them into a data file that has the same number of columns that the header file has rows.

header file
Code:
string1
string2
string3
string4
data file
Code:
 data11  data12  data13  data14
 data21  data22  data23  data24
 data31  data32  data33  data34
 data41  data42  data43  data44
 data51  data52  data53  data54
 data61  data62  data63  data64

etc.
output file
Code:
string1 string2 string3 string4
 data11  data12  data13  data14
 data21  data22  data23  data24
 data31  data32  data33  data34
 data41  data42  data43  data44
 data51  data52  data53  data54
 data61  data62  data63  data64

etc.
if I can flip the rows of the header file into columns then I can use your script

Code:
sed -i "1s/^/$(cat header)\n/" file
that worked really well to cat them with a column data file before, or I'm sure there's a way to modify the sed statement and do it all in one?
 
Old 12-12-2012, 02:12 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
What about a little modification to the embedded command substitution?
Code:
sed -i "1s/^/$(cat header|tr "\n" " ")\n/" file
 
Old 12-13-2012, 09:53 AM   #7
atjurhs
Member
 
Registered: Aug 2012
Posts: 190

Original Poster
Rep: Reputation: Disabled
thanks colucix!!!

I knew about the tr function, I just didn't know that I needed to follow it with
Code:
"\n" " "
thanks again!!! you've helped me out several times and I keep learning from your examples

Tabby
 
Old 12-13-2012, 11:22 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
You're welcome!
 
  


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
Adding custom info to TCP Header Options by changing Kernel Source code abhishek@LQ Linux - Networking 0 08-26-2010 11:29 PM
Create header file for existing .c file hk_michael Programming 5 02-25-2005 05:26 PM
Adding Option field in IP header guam Linux - Networking 0 11-04-2004 10:17 AM
[c] adding header to textfile jb_li Programming 6 02-29-2004 01:17 AM

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

All times are GMT -5. The time now is 10:46 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration