Insert string in alternate lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert string in alternate lines
# 1  
Old 02-03-2011
Insert string in alternate lines

Hi All,

In continuation of my previous thread 'Add text at the end of line conditionally', I need to further modfiy the file after adding text at the end of the line. Now, I need to add a fixed charater string at alternate lines starting from first line using awk or sed.My file is now as below:

Code:
cat myfile

aaaa.txt
bbbb.txt
cccc.txt

VAR="This is a new file"
I have the string stored in the variable VAR and I need the output as below:

Code:
cat myfile

This is a new file
aaaa.txt
This is a new file
bbbb.txt
This is a new file
cccc.txt

Regards
Angshuman
# 2  
Old 02-03-2011
Try:
Code:
sed "s/^/$VAR\n/" file

# 3  
Old 02-03-2011
Hi Scrutinizer,

It is inserting the value but not adding a new line. Actually, I tried this earlier also but it did not work. Here is what I did:

Code:
sed "s/^/$VAR\n/" myfile >tempfile.txt
mv tempfile.txt myfile

cat myfile

This is a new filenaaaa.txt
This is a new filenbbbb.txt
This is a new filencccc.txt

Regards
Angshuman
# 4  
Old 02-03-2011
Code:
awk -vvar=$VAR '{print var;print $0}' file

This User Gave Thanks to ghostdog74 For This Post:
# 5  
Old 02-03-2011
@angshuman. Are you on Solaris? Use /usr/xpg4/bin/sed instead of sed.
# 6  
Old 02-03-2011
Hi Scrutinizer,
I am on HP Unix
Regards
Angshuman
# 7  
Old 02-03-2011
You probably need to use an actual linefeed character:
Code:
sed "s/^/$VAR^M/" myfile >tempfile.txt

^M is entered as CTRL-V <ENTER>
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing alternate lines of code

Hi gents, Have only a passing familiarity with linux/shell at this point, so please forgive simple question. I have text files that have lines something like the following: a b c d d d e f e f e f a b (6 Replies)
Discussion started by: cabled
6 Replies

2. Shell Programming and Scripting

Insert String every n lines, resetting line counter at desired string

I need to read a text file and insert a string every n lines, but also have the line counter restart when I come across a header string. Line repeating working every 3 lines using code: sed '0~3 s/$/\nINSERT/g' < INPUT/PATH/FILE_NAME.txt > OUTPUT/PATH/FILE_NAME.txt I cannot seem to find... (1 Reply)
Discussion started by: Skonectthedots
1 Replies

3. Shell Programming and Scripting

Process alternate lines in awk/sed/perl

hi.. i have a fasta file with the following format >sequence1 CCGGTTTTCGATTTGGTTTGACT >sequence2 AAAGTGCCGCCAGGTTTTGAGTGT >sequence3 AGTGCCGCAGAGTTTGTAGTGT Now, i want to read alternate line and add "GGGGGGGGGGG" to end of every sequence Desired output: >sequence1... (4 Replies)
Discussion started by: empyrean
4 Replies

4. Shell Programming and Scripting

Grep values on alternate lines

Hi, I have a file like 2011|ACC|.* 2013|ACC|.* 2011|ACCC|.* 2013|ACCC|.* 2013|ACCV|.* 2011|ADB|.* 2013|ADB|.* 2011|ADBC|.* 2013|ADBC|.* 2011|AIA|.* 2013|AXJ|.* 2013|NNN|.* .* represnts any alphanumeric characters after this part of the string I need a code to return only the... (3 Replies)
Discussion started by: sam05121988
3 Replies

5. Programming

Perl : joining alternate lines

Hi, I need to join every alternate line in a file for eg:input file $ cat abc abc def ghi jkloutput abc def ghi jklcode i wrote for this $ cat add_line.pl #!/usr/bin/perl -w my $count=1; #my $line=undef; my @mem_line; my $i=0; my $x=0; (2 Replies)
Discussion started by: sam05121988
2 Replies

6. Shell Programming and Scripting

Insert a string instead of blank lines

how can i insert a string sush as "###" instead of blank lines in a file? i try this code but it doesn't work! awk 'NF<1 {$1=="###" ; print$0}' in_file > out_file (13 Replies)
Discussion started by: oreka18
13 Replies

7. UNIX for Dummies Questions & Answers

Insert Text on lines having the string word

I need help on how I can accomplish my task. I hope someone can help me since I've researching and trying to accomplish this for hours now. Basically, I need to comment-out (or insert a # sign in the beginning of the line) a line when the line has the specific word I am searching. Example I have... (3 Replies)
Discussion started by: Orbix
3 Replies

8. Shell Programming and Scripting

reading alternate lines of a file

hi, i have 2 files. file1: 1 2 3 4 5 6 file2: a b c d e f g h i (5 Replies)
Discussion started by: vidyaj
5 Replies

9. Shell Programming and Scripting

alternate lines

Hi, I'm new to Unix. I want to read the all the lines from a text file and write the alternate lines into another file. Please give me a shell script solution. file1 ----- one two three four five six seven newfile(it should contain the alternate lines from the file1) ------- one... (6 Replies)
Discussion started by: pstanand
6 Replies

10. UNIX for Dummies Questions & Answers

alternate lines from two files

A basic request two files want to combine them but on alternate lines (1 Reply)
Discussion started by: SummitElse
1 Replies
Login or Register to Ask a Question