How to append text to the second line of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to append text to the second line of a file
# 1  
Old 11-06-2010
How to append text to the second line of a file

Say I have a text file like:

Code:
1
3
4

How would I use ksh to put the number '2' into the second line of that file? I'm using OpenBSD so the sed syntax might be a bit different (I have no idea how to use sed, though)

Last edited by guitarscn; 11-06-2010 at 07:45 PM..
# 2  
Old 11-06-2010
Hi,

Here a 'sed' script. I don't know if it could work under a BSD. My machine is a Linux one.
Code:
$ cat infile
1
3
4

Code:
$ cat script.sed
2 i\
2

Code:
$ sed -f script.sed infile
1                                                                                                                                                                                                                 
2                                                                                                                                                                                                                 
3                                                                                                                                                                                                                 
4

Regards,
Birei
This User Gave Thanks to birei For This Post:
# 3  
Old 11-06-2010
In my opinion, this is a job for awk rather than sed.

Assuming that tokens are numeric, and are always column one, then:

Code:
#!/usr/bin/env ksh
awk '
        {
                if( last )
                        for( i = last + 1; i < $1+0; i++ )
                                printf( "%d\n", i );
                last = $1+0;
                printf( "%s\n", $0 );
        }
' <input-file >output-file

Quote:
How would I use ksh to put...
Purely in Ksh:

Code:
#!/usr/bin/env ksh

last=0
while read x junk
do
        for (( i=$(( $last + 1 )); i < $x; i++ ))
        do
                printf "%d\n" $i
        done

        last=$x
        print $x $junk
done <input >output

(Read allows for more than one token on the line, hence the junk variable)

---------- Post updated at 19:18 ---------- Previous update was at 19:17 ----------

Of course birei beat me with a sed example Smilie
This User Gave Thanks to agama For This Post:
# 4  
Old 11-07-2010
Code:
awk 'NR==2{$0="2" RS $0}1' infile

# 5  
Old 11-08-2010
Code:
sed '2s/^/2\n/' infile

---------- Post updated at 09:18 ---------- Previous update was at 09:07 ----------

ksh:
Code:
#!/bin/ksh
while read -r line; do
  if (( ++i == 2 )); then
    echo 2
  fi
  printf "%s\n" "$line"
done < infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to update file with partial matching line in another file and append text

In the awk below I am trying to cp and paste each matching line in f2 to $3 in f1 if $2 of f1 is in the line in f2 somewhere. There will always be a match (usually more then 1) and my actual data is much larger (several hundreds of lines) in both f1 and f2. When the line in f2 is pasted to $3 in... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Append Text File from another File in line 6

Hi, Anyone can help on how to append a file1.txt into file2.txt after line 3 using sed command. file1.txt 1. testa 2. testb 3. testc 4. testd 5. test5 6. test6 file2.txt break here this is a test break end here output 1. testa 2. testb (1 Reply)
Discussion started by: fspalero
1 Replies

3. Shell Programming and Scripting

Needed shell script to append desired text to each line in a file

Hi, I had generated a report in my tool as followsoutput.txt 43.35 9 i needed the script to generate a new file like below i want to append the text to each of these lines of my filenewoutputfile.txt should be Total Amount : 43.35 Record Count:9 Regards, Vasa Saikumar. ... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

4. UNIX for Dummies Questions & Answers

Append a line to single column text file

I would like to add a line to the end of a single column text file. How do I go about doing that? Input: BEGIN 1 2 3 Output: BEGIN 1 2 3 END Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

5. Shell Programming and Scripting

find a certain line and append text to the end of the line

After I create printer queues in AIX, I have to append a filter file location within that printers custom file. within lets say test_queue.txt I need to find the row that starts with :699 and then I need to append on the end the string /usr/local/bin/k_portrait.sh. Now I've gotten the sed... (2 Replies)
Discussion started by: peachclift
2 Replies

6. Shell Programming and Scripting

Append text to end of every line

I've scoured the internet with mixed results. As an amateur I turn to the great minds here. I have a text file of 80 or so lines. I want to add ".pdf" to the end of each line. (For now that's it) Most of the internet points toward using "sed". I don't know coding but can figure things out... (4 Replies)
Discussion started by: spacebase
4 Replies

7. Shell Programming and Scripting

append each line with text

hi, I've a file with some text in it, i need to append few strings in the beginning and end of each row. --in file richie matt .. --out file hi, 'richie' is here hi, 'matt' is here ... I tried with awk command, but it fails because of ' Thanks (2 Replies)
Discussion started by: dvah
2 Replies

8. Shell Programming and Scripting

append delimeter count for each line in text file

Hi guys, plz tell me how to achieve this how to delete the lines in a file using sed command (6 Replies)
Discussion started by: hari908
6 Replies

9. UNIX for Dummies Questions & Answers

sed - append text to every line

Hi all I tried this on an old version of sed on NCR Unix MP-RAS: sed -e "s/$/nnn/" file1 >file2 This file (file1): the cat sat on the mat. the cat sat on the mat. the cat sat on the mat. becomes this (file2): the cat sat on the mat.nnn the cat sat on the mat.nnn nnn the... (3 Replies)
Discussion started by: jgrogan
3 Replies

10. Shell Programming and Scripting

Append text at end of the first line in a file

Hi I need to append some text @ end of the first line in a file. like myfile.txt list = a,b,c list.a=some.. I give the arg "d" . now it append at end of first line list=a,b,c,d list.a=some... Please help me out this (7 Replies)
Discussion started by: catgovind
7 Replies
Login or Register to Ask a Question