LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-31-2014, 01:24 PM   #1
smturner1
Member
 
Registered: Oct 2009
Location: MI
Distribution: Arch 2.6.35
Posts: 107

Rep: Reputation: 1
How do I prepend a header into an already existing file?


I rebuilt a bunch of files (65 or so) that someone deleted for a specific customer, subaccount. All of the records are in the files with their related file name. Now I have to prepend the header and append the trailer to each one of these files.

As you all are aware that '>>' does not prepend files. I am lost because I know that awk and/or sed probably has the solution I am looking for but it is evading me.

I would appreciate any help with the following shell.

Code:
 cat <file_list> | while read file_name; do
 zcat $file_name | head -1 | while read hdr; do
 
     echo $hdr | awk {something goes here, copy to $file_name} 
 done
zcat  $file_name | tail -1 >> $file_name
Thank you for your help in advance.
 
Old 03-31-2014, 01:48 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Why not just put it into a new file?

head -1 $file_name > new_file
cat existing_file >> new_file
tail -1 $file_name >> new_file
 
1 members found this post helpful.
Old 03-31-2014, 01:48 PM   #3
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
Seems like for each file you want to create a header and trailer file, then in your inner loop do:

Code:
cat $header $file_name $trailer > $tmpfile ; mv -f $tmpfile $file_name
It's not clear to me from your post where you are getting your header and trailer from.
 
1 members found this post helpful.
Old 03-31-2014, 02:04 PM   #4
smturner1
Member
 
Registered: Oct 2009
Location: MI
Distribution: Arch 2.6.35
Posts: 107

Original Poster
Rep: Reputation: 1
Suicidaleggroll - I could do that if all I was looking for is a quick and tedious solution.

smallpond - Let me see if I can clear this us a little.


Code:
cat <file_list> | while read file_name; do
 zcat $file_name | head -1 | while read hdr; do
 
     echo $hdr | awk {something goes here, copy to $file_name} 
 done
zcat  $file_name | tail -1 >> $file_name
file_list = a list of file names in a given directory (basically an array).
file_name = one of the files in "file_list" (basically a scalar). All of the files that are read to this var are compressed.

I hope this clears things up.

Two things to note here; 1 - I am not looking for a solution that "just" works (that's easy). 2 - I want clean efficient code that is reusable.
 
Old 03-31-2014, 05:01 PM   #5
smturner1
Member
 
Registered: Oct 2009
Location: MI
Distribution: Arch 2.6.35
Posts: 107

Original Poster
Rep: Reputation: 1
I guess I will concede, since there are no other options. Thank you both for your input....


Here is the solution I used (which is basically the same as the two contributors above).

Code:
cat file.txt | while read file_name; do
   cat $file_name | head -1 | while read hdr; do
    
      echo $hdr > temp_file
      cat $file_name | grep -i foo | grep T1234 >> temp_file
      cat $file_name | tail -1 >> temp_file
      cat temp_file > $file_name
      done
  
   rm temp_file
   done
PS - I am still looking for someone who may have a more elegant approach.
 
Old 04-01-2014, 10:21 AM   #6
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
In that case, here is a slightly more elegant solution. NR == 1 selects first line, tail holds last line at END, regexes select your body lines. One gotcha is that your trailer line must not match a regex or it will print twice.

Code:
cat file.txt | while read file_name; do
    awk '{tail=$0} NR == 1 || /foo/ || /T1234/ {print $0} END {print tail}' $file_name > temp_file
    mv -f temp_file $file_name
done
 
1 members found this post helpful.
Old 04-01-2014, 10:43 AM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
sed can also prepend in-place with the -i and 1i flags (see below). However, internally it still does the same write-to-temp-file-and-move-back-into-place operation that the above steps do.

Code:
sed -i '1i Line to prepend' file.txt
And you should really get rid of that unnecessary second loop and all of those useless cats, they just complicate the code and slow things down with absolutely no benefit:
Code:
while read file_name; do
   head -1 $file_name > temp_file
   grep -i foo $file_name | grep T1234 >> temp_file
   tail -1 $file_name >> temp_file
   mv temp_file $file_name
done < file.txt

Last edited by suicidaleggroll; 04-01-2014 at 10:53 AM.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Prepend a header without copying the whole file loluengo Linux - Software 5 12-09-2011 02:23 PM
[SOLVED] [tar] Do not prepend subdir name to each file? littlebigman Linux - Software 6 06-25-2011 07:05 AM
want to prepend to a text file, not append urentity Programming 7 08-03-2009 12:45 AM
How to prepend header to sendfile? vargadanis Programming 0 08-07-2008 02:04 PM
Create header file for existing .c file hk_michael Programming 5 02-25-2005 05:26 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:44 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
Open Source Consulting | Domain Registration