Add to the second line of the file first character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add to the second line of the file first character
# 1  
Old 06-24-2018
Add to the second line of the file first character

I have a file:
Code:
H,20180620093911,VPAS,TRANSHDR,20180620,VPAS.TRANS_HDR.20170731.01.txt,
 ,876,496737dd53431ef666cad1a3511b3c5a8912f505bf6edfb9d0e225fb049ce934,
T,1,


I need to add to the second line

I have a command sed, how can I find the second line?


Thanks for contribution
# 2  
Old 06-24-2018
For example to replace the first character on line 2 with an "X", try:
Code:
sed '2s/^./X/' file

Code:
H,20180620093911,VPAS,TRANSHDR,20180620,VPAS.TRANS_HDR.20170731.01.txt,
X,876,496737dd53431ef666cad1a3511b3c5a8912f505bf6edfb9d0e225fb049ce934,
T,1,

To insert a character "X" before the 2nd line:
Code:
sed '2s/^/X/' file

Code:
H,20180620093911,VPAS,TRANSHDR,20180620,VPAS.TRANS_HDR.20170731.01.txt,
X ,876,496737dd53431ef666cad1a3511b3c5a8912f505bf6edfb9d0e225fb049ce934,
T,1,

# 3  
Old 06-24-2018
Quote:
Originally Posted by Scrutinizer
For example to replace the first character on line 2 with an "X", try:
Code:
sed '2s/^./X/' file

Code:
H,20180620093911,VPAS,TRANSHDR,20180620,VPAS.TRANS_HDR.20170731.01.txt,
X,876,496737dd53431ef666cad1a3511b3c5a8912f505bf6edfb9d0e225fb049ce934,
T,1,

To insert a character "X" before the 2nd line:
Code:
sed '2s/^/X/' file

Code:
H,20180620093911,VPAS,TRANSHDR,20180620,VPAS.TRANS_HDR.20170731.01.txt,
X ,876,496737dd53431ef666cad1a3511b3c5a8912f505bf6edfb9d0e225fb049ce934,
T,1,




I insert variable:
Code:
cnt=`wc -l $CONTROLFILE | awk '{print $1}'`
sed '2s/^/$cnt/' $CONTROLFILE


And no result.
Please advise


Thanks for contribution

---------- Post updated at 05:04 PM ---------- Previous update was at 04:57 PM ----------

Quote:
Originally Posted by digioleg54
I insert variable:
Code:
cnt=`wc -l $CONTROLFILE | awk '{print $1}'`
sed '2s/^/$cnt/' $CONTROLFILE

And no result.
Please advise







Thanks for contribution

Even I put:
Code:
sed '2s/^./3/' $CONTROLFILE


Anyway no result:
Code:
H,20180624170202,VPAS,TRANS_HDR,20180724,VPAS.TRANS_HDR.20180724.01.txt,
 ,93,
T,1,




I don't know what to do with it.


Thanks
# 4  
Old 06-24-2018
Shell variables are not expanded when they appear in a command line between single quotes. Try changing the single quotes in the sed command first argument:
Code:
cnt=`wc -l $CONTROLFILE | awk '{print $1}'`
sed '2s/^/$cnt/' $CONTROLFILE

to double quotes:
Code:
cnt=`wc -l $CONTROLFILE | awk '{print $1}'`
sed "2s/^/$cnt/" $CONTROLFILE

# 5  
Old 06-25-2018
What do you mean by

Quote:
Originally Posted by digioleg54
. . .
Anyway no result:
Code:
H,20180624170202,VPAS,TRANS_HDR,20180724,VPAS.TRANS_HDR.20180724.01.txt,
 ,93,
T,1,

. . .

Is this the command's output or the file's contents after the command?
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 add a character after the first word/space on each line?

Hi:) I have a large file ( couple thousand lines ) and I'm trying to add a character after the first word/space on each line. eg: First line of text Second line of text Third line of text Fourth line of text Fifth line of text I'd like to accomplish: First - line of text Second... (8 Replies)
Discussion started by: martinsmith
8 Replies

2. Shell Programming and Scripting

Conditionally add character at end of line

Hi, I would like have a shell script to check every line in a file to see if it ends with ";". If this is NOT the last character ";" should be added. MyFile.csv : web9331801;01/01/2014 23:39:35;;"93962";353150256; web9331802;01/01/2014 23:44:29;;"479288";353153538; web9331803;01/01/2014... (14 Replies)
Discussion started by: vg77
14 Replies

3. Shell Programming and Scripting

How to add a character at end of line?

Hai, I have got a small requirement in my script. and i am using bash shell. I need to add a dot (.) for some particular line in a file. Say for example, $Cat rmfile 1 This is line1 2 This is line2 3 This is line3 O/p should be : $Cat rmfile 1 This is line1 2 This is line2. #... (2 Replies)
Discussion started by: Sivajee
2 Replies

4. Shell Programming and Scripting

Remove new line character and add space to convert into fixed width file

I have a file with different record length. The file as to be converted into fixed length by appending spaces at the end of record. The length should be calculated based on the record with maximum length in the file. If the length is less than the max length, the spaces should be appended... (4 Replies)
Discussion started by: Amrutha24
4 Replies

5. Shell Programming and Scripting

Add new line before specific character

hello all, I have file like this "infile.txt" (all data in one line) A240 582247.99974288.7 8.0 239 582248.19974301.1 8.0 238 582248.39974313.6 8.01A 237 582248.49974326.1 8.0 236 582248.69974338.6 8.0 235 582248.79974351.1 8.01A 234 582248.99974363.5 8.0 233 582249.19974376.0 8.0 232... (4 Replies)
Discussion started by: attila
4 Replies

6. HP-UX

How to remove new line character and append new line character in a file?

Hi Experts, I have data coming in 4 columns and there are new line characters \n in between the data. I need to remove the new line characters in the middle of the row and keep the \n character at the end of the line. File is comma (,) seperated. Eg: ID,Client ,SNo,Rank 37,Airtel \n... (8 Replies)
Discussion started by: sasikari
8 Replies

7. Shell Programming and Scripting

add character to every end-of line in file

Hi All I have a file which conatins record.the length of every records is 47. problem : in the end of record i don't have a "\015" character. i want to add this "\015" charcter in the end of every record. the file contains something like 700 records. i've tried with sed command - nothing. ... (8 Replies)
Discussion started by: naamas03
8 Replies

8. Shell Programming and Scripting

add character to the end of each line in file

hi all i have 32 lines in file. the length of each line is 82 , i want that in the end of each line , means in postion 83-84 to put two characters 0d(=\015), 0a(=\012) i want that the 0d will be in postion 83 and the 0a will be in postion 84 in each line of the file how shall i do it ? ... (7 Replies)
Discussion started by: naamas03
7 Replies

9. UNIX for Dummies Questions & Answers

How to add new line character at the end of a file

hi all, i have this question: How to add new line character at the end of a file???? i need this because i am loading a file to sybase and i have problems with the last record thanks for your help (5 Replies)
Discussion started by: DebianJ
5 Replies

10. Shell Programming and Scripting

how to add new line character

Hello All, I want to add new line character to each of the line '\n'.what is the unix command i can use to do that.File is like below a|b|c c|d|f (3 Replies)
Discussion started by: ravi.sadani19
3 Replies
Login or Register to Ask a Question