How to input text on every 2nd line?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to input text on every 2nd line?
# 1  
Old 04-06-2010
How to input text on every 2nd line?

I have a file in two columns consisting of only numbers. I would like to put a string (the same textstring) on every second line, so that

501006 420.000
401602 165.000
.
.
.

becomes:

*Some_text
501006 420.000
*Some_text
401602 165.000
*Some_text
.
.
.

Are you up for it?
Thx...
# 2  
Old 04-06-2010
Code:
awk ' { print $0; print "*some text"} ' inputfile > outputfile

# 3  
Old 04-06-2010
Using awk you can write:

Code:
awk '{ print; print "*Some_text"; }' input_file

# 4  
Old 04-06-2010
Code:
$
$
$ cat f7
501006 420.000
401602 165.000
123456 789.012
$
$ perl -pne '$_.="AND_SOME_TEXT_HERE\n"' f7
501006 420.000
AND_SOME_TEXT_HERE
401602 165.000
AND_SOME_TEXT_HERE
123456 789.012
AND_SOME_TEXT_HERE
$
$

tyler_durden

To add the text on first line as well -

Code:
$
$ perl -pne 'BEGIN{$t="AND_SOME_TEXT_HERE"} $.==1 ? $_="$t\n$_" : $_.="$t\n"' f7
AND_SOME_TEXT_HERE
501006 420.000
AND_SOME_TEXT_HERE
401602 165.000
AND_SOME_TEXT_HERE
123456 789.012
AND_SOME_TEXT_HERE
$
$


Last edited by durden_tyler; 04-06-2010 at 11:33 AM..
# 5  
Old 04-06-2010
Code:
sed 'i\
*Some_text
' file

# 6  
Old 04-06-2010
Quote:
Originally Posted by alister
Code:
sed 'i\
*Some_text
' file

Hi,

sorry, but you must use the command a (append). Smilie

Greeting
# 7  
Old 04-06-2010
Quote:
Originally Posted by grepFruit
Hi,

sorry, but you must use the command a (append). Smilie

Greeting
Welcome to the forums, grepfruit (love the nick Smilie).

If you look at the sample data provided, you'll see that the inserted text precedes each line. That is not an append.

Regards,
Alister

Last edited by alister; 04-06-2010 at 01:44 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search last column of INPUT.txt in TABLEs text and add correspond columns to INPUT.txt

Hi dears i use bash shell i have INPUT.txt like this number of columns different in one some row have 12 , some 11 columns see last column INPUT.txt CodeGender Age Grade Dialect Session Sentence Start End Length Phonemic Phonetic 63 M 27 BS/BA TEHRANI 3 4 298320 310050... (2 Replies)
Discussion started by: alii
2 Replies

2. UNIX for Dummies Questions & Answers

Search String, Out matched text and input text for no match.

I need to search a string for some specific text which is no big deal using grep. My problem is when the search fails to find the text. I need to add text like "na" when my search does not match. I have tried this command but it does not work when I put the command in a loop in a bash script: ... (12 Replies)
Discussion started by: jojojmac5
12 Replies

3. Shell Programming and Scripting

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

4. Shell Programming and Scripting

Deleting text start at 2nd line....

How can I delete the first '2' characters in the 2nd line and every fourth line thereafter (Means line no.s 2, 6, 10...so on). For example if the following is my Input treieieiei trrpepepepep tyeueueue tyeueueeu tyeyeyeye tryryry tyeyey tyrtyty tytyty tututu tututututu Output... (8 Replies)
Discussion started by: cs_novice
8 Replies

5. Shell Programming and Scripting

Curl - input line by line from text file

Hi, I've got a text file with hundreds of lines I need to upload to an API via curl, one by one. The text file is like: 2012-08-01 10:45,124 2012-08-02 10:45,132 2012-08-03 10:45,114 I want to get curl to go through the text file sending a post for each line. like: curl --request... (0 Replies)
Discussion started by: emdeex
0 Replies

6. UNIX for Dummies Questions & Answers

How to read a line of text from user input?

Hiii I wanna a read a line of text from standard input. The user enter data like this way name phone_no month1_salary month2_salary that is user enter the name ,phone no and salary of 2 months in a single line by giving spaces. I wanna add the 3rd and 4th fields ...ie add both... (4 Replies)
Discussion started by: krishnampkkm
4 Replies

7. Shell Programming and Scripting

print line if 2nd field exists in text

2 files, first one has 3 fields seperated by ||| and 2nd one is plain text. I want to copy the lines from the first file if the 2nd field is present anywhere in the text file. This is what I've tried, but I'm new to awk and shell scripting in general so it's kinda broken. #!/bin/awk -f BEGIN... (15 Replies)
Discussion started by: FrancoisCN
15 Replies

8. UNIX for Dummies Questions & Answers

add new lines of text before and after each input line

I have a file that contains hundreds of lines such as: this_is_macro,000001 this_is_macro,000002 this_is_macro,000003 I would like to add the variable words MACROBEGIN MACRO_000001 MACROBEGIN MACRO_000002 MACROBEGIN MACRO_000003 above each line and add the word MACROEND ... (2 Replies)
Discussion started by: kenneth.mcbride
2 Replies

9. Shell Programming and Scripting

How to print the lines after 2nd line from text file

Hi All, I need to print the lines after 2nd line irrespective of the number of lines in file if the file contents are like below ---------- root:!:0:0::/root:/usr/bin/ksh daemon:!:1:1::/etc: bin:!:2:2::/bin: sys:!:3:3::/usr/sys: adm:!:4:4::/var/adm: uucp:!:5:5::/usr/lib/uucp:... (6 Replies)
Discussion started by: ram1729
6 Replies

10. Shell Programming and Scripting

How to prompt for input & accept input in ONE line

hi, am a new learner to shell programming. i have a script which will prompt for user to key in their name & display their name afterwards. script ===== echo "Pls enter your name:" read name echo "Your name is $name." output ===== Pls enter your name: Bob Your name is Bob. what... (2 Replies)
Discussion started by: newbie168
2 Replies
Login or Register to Ask a Question