Want to add a word in front a of each line of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Want to add a word in front a of each line of a file
# 1  
Old 04-08-2009
Want to add a word in front a of each line of a file

Hi,

Can anybody help me how to add a word in front of a line in a file.Actually it is bit tricky to add a word. i will give a sample for this:

Input :

1110001 ABC DEF
1110001 EFG HIJ
1110001 KLM NOP
1110002 QRS RST
1110002 UVW XYZ

Output:
%HD% 1110001 ABC DEF
%DT% 1110001 EFG HIJ
%DT% 1110001 KLM NOP
%HD% 1110002 QRS RST
%DT% 1110002 UVW XYZ

you see...i need to add %HD% for a unique number when it encounters in each line and then successvely add %DT% till another unique number encounters.

Please help me to meet this requirement.

Thanks in advance.

Best Regards,
Neeraj
# 2  
Old 04-08-2009
What have you tried so far?

Regards
# 3  
Old 04-08-2009
Actually i am a newbie, so could go so far ....
but i was thinking to add first %HD% for all unique numbers as soon as they encounter and in second phase to add %DT% everywhere else.

This is what i was logically derived because in one go it is difficult to find the answer for me.

Thanks for your reply.

Neeraj
# 4  
Old 04-08-2009
Should be something like this:

Code:
awk '!a[$1]++{print "%HD%", $0; next}{print "%DT%", $0}' file

Regards
# 5  
Old 04-09-2009
Thank you very much...It worked fine.....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

2. UNIX for Dummies Questions & Answers

Add word/value at the beginning of each line in a file

how to add value/word at the beginning of each line in a file ? i have file number.txt and the output is below 1000 1001 1002 1003 1004 i want to add 000 at the beginning of each line, desire output is below 0001000 0001001 0001002 0001003 0001004 and so on please advise how... (5 Replies)
Discussion started by: jason6247
5 Replies

3. Shell Programming and Scripting

Fetch entries in front of specific word till next word

Hi all I have following file which I have to edit for research purpose file:///tmp/moz-screenshot.png body, div, table, thead, tbody, tfoot, tr, th, td, p { font-family: "Liberation Sans"; font-size: x-small; } Drug: KRP-104 QD Drug: Placebo Drug: Metformin|Drug:... (15 Replies)
Discussion started by: Priyanka Chopra
15 Replies

4. Shell Programming and Scripting

add a word every other line

hey guys, How to add a line to every other line, means adding a line to the above of all the lines of a file? tnx (4 Replies)
Discussion started by: Johanni
4 Replies

5. Shell Programming and Scripting

Script for adding a word in front of all line in a file

Hi I've one file full of paths of certain files and I want to add some extra file words in front of all the paths. for eg: i have a file name test.txt which show some details only.. 024_hd/044/0344eng.txt 035_bv/222/editor.jpg here I want to add /usr/people/indiana/ infront of all the... (4 Replies)
Discussion started by: ratheeshp
4 Replies

6. Shell Programming and Scripting

how to add single digit in front of the word and line in the file.

Hi , how to add the single digit to front of the word and front of the lines in the one file with compare pattern file and get digit. like example pattern file pattern.txt pattern num bala 2 raja 3 muthu 4 File Name: chennai.dat muthu is good boy raja is bad boy selvam in super... (6 Replies)
Discussion started by: krbala1985
6 Replies

7. Shell Programming and Scripting

How join line and add | in front

Hello, Did any one know how to use perl join line and add | in front Input--> timestamp=2009-11-10-04.55.20.829347; a; b; c; ddaa; timestamp=2009-11-10-04.55.20.829347; aa; bb; cc; Output--> ... (2 Replies)
Discussion started by: happyday
2 Replies

8. Shell Programming and Scripting

Adding a word in front of a word of each line.

Adding a word in front of a word of each line.In that line only one word will be there. pl help:( (4 Replies)
Discussion started by: Ramesh Vellanki
4 Replies

9. Shell Programming and Scripting

Add a word at the end of each line in a file

Hi I want to read a flat file and add a word/value at the end of each line in the file and store the output in a temporary file. How can i do this? Plz help me with this. Regards, Saurabh (6 Replies)
Discussion started by: bhalotias
6 Replies

10. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies
Login or Register to Ask a Question