Adding a text in the beginning of a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding a text in the beginning of a line
# 1  
Old 01-26-2014
Adding a text in the beginning of a line

Hi,

I am doing something like below:

Code:
cat file1>file3

and
Code:
cat file2>>file3

I wanted to check if there is a way to write a custom message(hardcoded message)something like below at the beginning of each line then PIPE delimitiation and then followed by remaining record.

Code:
cat file1 >Custom Message THIS IS FROM FILE1" then PIPE " |" then remaining records from file1 > file3
 
cat file2 > " Custom Message THIS IS FROM FILE2" then PIPE " |" then remaining records from file2 >> file3

So the final file "file3" has custom mesages and records from file 1 and file 2.

Thanks
Moderator's Comments:
Mod Comment Please use CODE tags (not font and color changes) to show sample code, sample input data, and sample output data.

Last edited by Saanvi1; 01-27-2014 at 11:46 AM.. Reason: Change BOLD tags to CODE tags.
# 2  
Old 01-27-2014
This seems to do what you want:
Code:
awk -v outfile=file3 '
FNR == 1 {
        prefix = " Custom Message THIS IS FROM " toupper(FILENAME) " |"
}
{       print prefix $0 > outfile
}' file1 file2

If you want to try this on a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk, instead of /usr/bin/awk.
# 3  
Old 01-27-2014
Thanks Don for pointing this out. I will follow the code tags for all my future post. I am on Sun Solaris. I will try out your suggestion above.

I am not an awk geek and doesn't understand awk that well, I wanted to check if there is a little simpler way of achieving the same thing that I mentioned in my post.

I would really appreciate and thanks in advance.

Thanks
# 4  
Old 01-27-2014
This can be done thousands of ways on any UNIX System. Finding something simpler than a two line awk program may be difficult. If you're unwilling to use /usr/xpg4/bin/awk, what tools are you willing to use?

If you'd like to learn more about awk, and you can't figure out how this program works by reading the awk(1) man page, I'll be happy to explain what it is doing for you.

Last edited by Don Cragun; 01-27-2014 at 03:51 PM..
These 2 Users Gave Thanks to Don Cragun For This Post:
# 5  
Old 01-27-2014
Thanks Don.....Let me try out the AWK....As suggested by you, I will look into the MAN page as well.

Thank You so much for your reply and helping me out.

Thanks
# 6  
Old 01-27-2014
Maybe you are looking for something like this:
Code:
printf "%s |" "Custom Message THIS IS FROM FILE1" | cat - file1 > file3
printf "%s |" "Custom Message THIS IS FROM FILE2" | cat - file2 >> file3

# 7  
Old 01-27-2014
Quote:
Originally Posted by Scrutinizer
Maybe you are looking for something like this:
Code:
printf "%s |" "Custom Message THIS IS FROM FILE1" | cat - file1 > file3
printf "%s |" "Custom Message THIS IS FROM FILE2" | cat - file2 >> file3

This might be what Saanvi1 wants, but the original request was:
Quote:
I wanted to check if there is a way to write a custom message(hardcoded message)something like below at the beginning of each line then PIPE delimitiation and then followed by remaining record.
The code above puts the custom message at the beginning of each file rather than at the start of each line in the file.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert text at the beginning of every even number line

i am trying to insert text at the beginning of every even number line with awk i can do it with odd number lines with this command awk 'NR%2{$0="some text "$0}1' filehow can i edit this command thanks (5 Replies)
Discussion started by: bob123
5 Replies

2. Windows & DOS: Issues & Discussions

Trying to add text to the beginning of each line

Well here goes: I tried to write a batch file that adds a specific fixed text to each line of an already existing text file. for the adding text infront of each line I tried this: for /F "delims=" %%j in (list.txt) do echo.STARTTEXT\%%j >> list.txt for adding text after each line I... (6 Replies)
Discussion started by: pasc
6 Replies

3. Shell Programming and Scripting

trying to add text to beginning and end of each line

Well here goes: I tried to write a batch file that adds a specific fixed text to each line of an already existing text file. for the adding text infront of each line I tried this: for /F "delims=" %%j in (list.txt) do echo.STARTTEXT\%%j >> list.txt for adding text after each line I... (0 Replies)
Discussion started by: pasc
0 Replies

4. Shell Programming and Scripting

adding a line to a text file

I have a tab delimited text file, id name distance 1 3325167 0.334561754018 2 3290488 0.389444269458 3 3288794 0.392312701782 4 3347602 0.392532202097 5 3295355 0.394394169485 I need to add a line after the header line. The first and third field of... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

5. Shell Programming and Scripting

Adding text in final line

Dear Friends, I have a flat file where last line of it has word D$mhtt I want to add a space and back slash after it. Also wanna add -S "J" in the last line. Following example will make it clear. I have this in the last line of file D$mhtt I want D$mhtt \ -S "J" Please... (5 Replies)
Discussion started by: anushree.a
5 Replies

6. UNIX for Dummies Questions & Answers

Adding one string at the beginning of each line in a file

Hi, I have file a.txt as below. I want to add one string root beginning of each line. Sample file a.txt aaa bbb ccc Sample output Root aaa Root bbb Root ccc Can any one help me on this? (6 Replies)
Discussion started by: siba.s.nayak
6 Replies

7. Shell Programming and Scripting

Adding specific text and spaces to each line in a text file

Hi, I wanted to add specific text to each row in a text file containing three rows. Example: 0 8 7 6 5 5 7 8 9 0 7 9 7 8 9 0 1 2 And I want to add a 21 at the beginning of the first row, and blank spaces at the beginning of the second two rows. To get this: 21 0 8 7 6 5 5 7 8... (4 Replies)
Discussion started by: hertingm
4 Replies

8. Shell Programming and Scripting

Adding a character in the beginning of every line in a .dat file

How can i add a character(#) in the beginning of every line in a .dat file (2 Replies)
Discussion started by: Cool Coder
2 Replies

9. Shell Programming and Scripting

adding text to end of each line in a file

I'm needing to add a "hour:min" to the end of each line in a document. The document in this case is only going to be one line. if this inserts it at the end, what needs to be changed to add something at the end... /bin/echo "%s/^/$filler/g\nwq!" | ex -s $oFile Thank you... (2 Replies)
Discussion started by: cubs0729
2 Replies

10. Shell Programming and Scripting

Adding Text To each line of a file

How would I add text to the beginning of each line in a text file in a script right after the file is created from another text file. (4 Replies)
Discussion started by: cubs0729
4 Replies
Login or Register to Ask a Question