Adding header once every 5 lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding header once every 5 lines
# 8  
Old 03-03-2011
Quote:
Originally Posted by pravin27
Hi michaelrozar17,
Cluld you please explain me the SED code ?
The only trick here is with the operator ~. 1~5, it means match every 5th line starting from line 1.Similarly 0~3 - matches every 3rd line starting from line 0. Rest of the thing i guess you are familiar..
This User Gave Thanks to michaelrozar17 For This Post:
# 9  
Old 03-03-2011
Note: ~ is GNU sed only.

Some other seds:
Code:
sed 's/^/\nNew New\nS.No Name\n(XX) (Y)\n&/;n;n;n;n' infile

---------- Post updated at 14:43 ---------- Previous update was at 14:41 ----------

Code:
awk '!(NR%5-1){print h}1' h="
New New
S.No Name
(XX) (Y)" infile


Last edited by Scrutinizer; 03-03-2011 at 10:34 AM..
# 10  
Old 03-03-2011
Error

@Scrutinizer,
thanks for your inputs.
but the sed is not able to take the \n charecters properly...
Code:
nNew NewnS.No Namen(XX) (Y)nn1  A
2  B

Can yu help with some othr option n sed?
Thanks,

Last edited by aravindan; 03-03-2011 at 10:10 AM..
# 11  
Old 03-03-2011
Quote:
Originally Posted by aravindan
nt sure why it fails on AIX Smilie Smilie
Just replace i==1?str$1
with (i==1)?str$1
This User Gave Thanks to Chubler_XL For This Post:
# 12  
Old 03-04-2011
Thanks a ton ChublerXL
Your idea worked like magic..
# 13  
Old 03-04-2011
Quote:
Originally Posted by Scrutinizer
Note: ~ is GNU sed only.

Some other seds:
Code:
sed 's/^/\nNew New\nS.No Name\n(XX) (Y)\n&/;n;n;n;n' infile

\n in the replacement text is also a non-portable (non-posix) extension Smilie. A backslash followed by a newline is the standardized method of including a literal newline in that section of a substitution command (\n is allowed in the regular expression).

Regards,
Alister
# 14  
Old 03-04-2011
Lightbulb

All,
I tried one option today.
Code:
cat sam.awk
  BEGIN{ printf "TEST" }
  {
   for(i=0;i<5;i++)
   { REPNUM=i; printf "REP: %d\n", REPNUM;
     LINE1="TEST THE PRINT value of REP: $REPNUM"
      printf " %s\n", ENVIRON["LINE1"]
   }
   }

I executed
Code:
# nawk -f sam.awk

& the output is :
Code:
TEST
REP: 0
REP: 1
REP: 2
REP: 3
REP: 4

But I am not able to print the LINE1...
Not getting a clue y??? Smilie
Can you please help me?

Last edited by Franklin52; 03-04-2011 at 08:33 AM.. Reason: Please use code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find header in a text file and prepend it to all lines until another header is found

I've been struggling with this one for quite a while and cannot seem to find a solution for this find/replace scenario. Perhaps I'm getting rusty. I have a file that contains a number of metrics (exactly 3 fields per line) from a few appliances that are collected in parallel. To identify the... (3 Replies)
Discussion started by: verdepollo
3 Replies

2. Shell Programming and Scripting

Need help in adding header of a file

Hi All , I have a pipe dilimited file .Sample file is below.I need to add header in that file through unix. 000001| 1|AQWWW|234,456.00 | | 123456| |41|abC| 0|xyZ| 000002| 2|11 4|1,234,456.99| | 0| |23| |99|! | 000003| 3|!!@#$|0,000,001.10| | ... (4 Replies)
Discussion started by: STCET22
4 Replies

3. UNIX for Dummies Questions & Answers

oneliner for adding header and trailer

for example, i have a file with below content: 123413 866688 816866 818818 i want the output as: This is header 123413 866688 816866 818818 This is trailer i am able to achieve it using a bash script. (2 Replies)
Discussion started by: pandeesh
2 Replies

4. Shell Programming and Scripting

adding header in a file

Hi team, In my script i am storing some value in a variable "header". I want to add the header value has header in a file. Please help me on this Thanks in advance, Baski (4 Replies)
Discussion started by: baskivs
4 Replies

5. UNIX for Dummies Questions & Answers

Adding header and trailer into a file

Hi, I want to add the below Header to all the files in sequence File1,File2,File3...etc "ABC,<number of chracter in the file>" e,g - If File1 is as below pqrstuvdt abcdefgh then I want to add the above header into it ,So that File1 becomes as below ABC,17 pqrstuvdt abcdefgh ... (9 Replies)
Discussion started by: spari2
9 Replies

6. UNIX for Dummies Questions & Answers

Rename a header column by adding another column entry to the header column name

Hi All, I have a file example.csv which looks like this GrpID,TargetID,Signal,Avg_Num CSCH74_1_1,2007,61,256 CSCH74_1_1,212007,647,679 CSCH74_1_1,12007,3,32 CSCH74_1_1,207,299,777 I want the output as GrpID,TragetID,Signal-CSCH74_1_1,Avg_Num CSCH74_1_1,2007,61,256... (1 Reply)
Discussion started by: Vavad
1 Replies

7. Shell Programming and Scripting

Rename a header column by adding another column entry to the header column name URGENT!!

Hi All, I have a file example.csv which looks like this GrpID,TargetID,Signal,Avg_Num CSCH74_1_1,2007,61,256 CSCH74_1_1,212007,647,679 CSCH74_1_1,12007,3,32 CSCH74_1_1,207,299,777 I want the output as GrpID,TragetID,Signal-CSCH74_1_1,Avg_Num CSCH74_1_1,2007,61,256... (4 Replies)
Discussion started by: Vavad
4 Replies

8. Shell Programming and Scripting

Adding new lines to a file + adding suffix to a pattern

I need some help with adding lines to file and substitute a pattern. Ok I have a file: #cat names.txt name: John Doe stationed: 1 name: Michael Sweets stationed: 41 . . . And would like to change it to: name: John Doe employed permanently stationed: 1-office (7 Replies)
Discussion started by: hemo21
7 Replies

9. UNIX for Dummies Questions & Answers

Adding a header to a log file

Hello, I’m trying to add a row that will server as the header for a space separated file. 1-I have a number of files save in a directory 2- grep text path/*.log > newfile newfile looks like this Field1 Field2 Field3 Field4 Field1 Field2 Field3 Field4 Field1 Field2 Field3 Field4 Field1... (2 Replies)
Discussion started by: rene reivera jr
2 Replies

10. UNIX for Dummies Questions & Answers

Adding header to an existing file

Dear All, I need to add a header of one line to an already existing file. I know that it can be achieved by the following: echo "Header" > newfile cat file1 >> newfile But my problem is that file is huge and there is no space for creating a new file every time. Is there a way that I can... (5 Replies)
Discussion started by: shash
5 Replies
Login or Register to Ask a Question