Unix Script file to Append Characters before rows in file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix Script file to Append Characters before rows in file.
# 1  
Old 05-24-2010
Unix Script file to Append Characters before rows in file.

Hi Experts,

I am working on HP-UX. I am new to shell scripting. I would like to have a shell script which will prefix:

1. "H|" before first row of my file and,
2. "T" for all other rows of the file.

For Example - File before running the script
Code:
20100430|4123451810|218.50|TC
20100430    $150.00	INDIA	  1234567	2      3456789
20100430    $150.00	INDIA	  1234567	2      3456789
20100430    $150.00	INDIA	  1234567	2      3456789
20100430    $150.00	INDIA	  1234567	2      3456789

File After Running the script

H|20100430|4123451810|218.50|TC
T20100430 $150.00 INDIA 1234567 2 3456789
T20100430 $150.00 INDIA 1234567 2 3456789
T20100430 $150.00 INDIA 1234567 2 3456789
T20100430 $150.00 INDIA 1234567 2 3456789

Thanks in Advance,
Phani Akella.

Last edited by Yogesh Sawant; 05-24-2010 at 03:24 AM.. Reason: added code tags
# 2  
Old 05-24-2010
Hi
Try this:

Code:
awk '{if (NR==1) print "H|"$0; else print "T"$0;}' file

Guru.
# 3  
Old 05-24-2010
Code:
awk '{$0=((NR-1)?"T":"H|")$0}1' file

# 4  
Old 05-24-2010
Code:
sed '1s/^/H|/; 2,$s//T/' file

Regards,
Alister
# 5  
Old 05-24-2010
using Perl:
Code:
perl -pi -e 'if ($. == 1) { s/^/H/; } else { s/^/T/; }' file.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove new line characters from data rows in a Pipe delimited file?

I have a file as below Emp1|FirstName|MiddleName|LastName|Address|Pincode|PhoneNumber 1234|FirstName1|MiddleName2|LastName3| Add1 || ADD2|123|000000000 2345|FirstName2|MiddleName3|LastName4| Add1 || ADD2| 234|000000000 OUTPUT : ... (1 Reply)
Discussion started by: styris
1 Replies

2. UNIX for Beginners Questions & Answers

UNIX script to append multiple text files into one file based on pattern present in filaname

Hi All-I am new to Unix , I need to write a script. Can someone help me with a requirement where I have list of files in a directory, I want to Merge the files if a pattern of string matches in filenames? AAAL_555A_ORANGE1_F190404.TXT AAAL_555A_ORANGE2_F190404.TXT AAAL_555A_ORANGE3_F190404.TXT... (6 Replies)
Discussion started by: Shankar455
6 Replies

3. Shell Programming and Scripting

Append spaces the rows to make it into a required fixed length file

I want to make a script to read row by row and find its length. If the length is less than my required length then i hav to append spaces to that paritucular row. Each row contains special characters, spaces, etc. For example my file contains , 12345 abcdef 234 abcde 89012 abcdefgh ... (10 Replies)
Discussion started by: Amrutha24
10 Replies

4. Shell Programming and Scripting

i writing a unix script but i want to out put a file and append on it.

i have an existing script that is used to send an e-mail containing the alrams that appear on the server. But i need to create a daily log file containing all the alarms that was send that day. i tired to add at the and of the script a command, echo command but for some reason the file was... (1 Reply)
Discussion started by: ashraf_victory
1 Replies

5. Shell Programming and Scripting

connecting to table to extract multiple rows into file from unix script

I need to extract the data from oracle table and written the below code. But it is not working.There is some problem with the query and output is shown is No rows selected" . If I run the same query from sql developer there is my required output. And if I run the shell script with simple sql... (7 Replies)
Discussion started by: giridhar276
7 Replies

6. Shell Programming and Scripting

How to search and append words in the same file using unix scripting file operations

Hi , I have a file myhost.txt which contains below, 127.0.0.1 localhost 1.17.1.5 atrpx958 11.17.10.11 atrpx958zone nsybhost I need to append words only after "atrpx958" like 'myhost' and 'libhost' and not after atrpx958zone. How to search the word atrpx958(which is hostname) only,... (5 Replies)
Discussion started by: gsreeni
5 Replies

7. Shell Programming and Scripting

How to remove Ctlr-M characters from file from a unix script

There are 10 files present which have Ctlr-M characters appended to each line of all files. I have a unix script which processes the files in a loop. And there is an inner loop which processes each line in the file concerned. #inputFile is a variable which has the file name of the input... (2 Replies)
Discussion started by: akashtcs
2 Replies

8. UNIX for Advanced & Expert Users

Append the file in UNIX

Hello All, Could you please provide the solution to my following query I have some files in my directory. I need to amend the total number of lines to be added in the starting line of each file. For example, please refer the content of the file "file.dat" below. \h:\w #> cat file.dat... (1 Reply)
Discussion started by: sdosanjh
1 Replies

9. Shell Programming and Scripting

How to append a Value to all the rows in a file

Hi, We have Multiple source files which has some data, I need a K shell script which will append number '1' as the last character to all the lines of the first file and then increments the value and appends '2' to all the lines to the next file and so on. For Example Incoming file 1 ... (2 Replies)
Discussion started by: dsshishya
2 Replies

10. Shell Programming and Scripting

Need to append file name to all rows in in .csv file

Hi , Can some one help in appending the file name to all the rows in .csv files the current work is like this. This is adding a new line for file name, I need to append file name to all lines in .csv for i in `ls $filename*.csv` do echo "$i" > ./tmpfile cat "$i" >> ./tmpfile mv... (3 Replies)
Discussion started by: Satyagiri
3 Replies
Login or Register to Ask a Question