Insert text at the beginning of every even number line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert text at the beginning of every even number line
# 1  
Old 04-03-2016
Insert text at the beginning of every even number line

i am trying to
insert text at the beginning of every even number line
with awk

i can do it with odd number lines
with this command

Code:
awk 'NR%2{$0="some text "$0}1' file

how can i edit this command

thanks
# 2  
Old 04-03-2016
Do you mean something like:
Code:
awk 'NR%2 == 1{$0="some text "$0}1' file

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 04-03-2016
Quote:
Originally Posted by Don Cragun
Do you mean something like:
Code:
awk 'NR%2 == 1{$0="some text "$0}1' file

thanks for reply

i changed the == 1 to 2==0
now it works many thanks great forum


Code:
awk 'NR%2==0{$0="some text "$0}1' file





# 4  
Old 04-03-2016
Quote:
Originally Posted by bob123
thanks for reply

i changed the == 1 to 2==0
now it works many thanks great forum


Code:
awk 'NR%2==0{$0="some text "$0}1' file





Ouch. Yes. Sorry. I wasn't paying close enough attention.

I'm glad you got it to work.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 04-04-2016
Or you may use negation in your original command as follows:

Code:
awk '!NR%2{$0="some text "$0}1' file

# 6  
Old 04-04-2016
Quote:
Originally Posted by krishmaths
Or you may use negation in your original command as follows:

Code:
awk '!NR%2{$0="some text "$0}1' file

Not quite. In awk ! has higher precedence than % so that evaluates as (!NR)%2. But:
Code:
awk '!(NR%2){$0="some text "$0}1' file

would work.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Insert the line number from text file to filename output

Hi everyone :) I have a file "words.txt" containing hundreds of lines of text. Each line contains a slogan. Using the code below i am able to generate an image with the slogan text from each line. The image filename is saved matching the last word on each line. Example: Line 1: We do... (2 Replies)
Discussion started by: martinsmith
2 Replies

2. Windows & DOS: Issues & Discussions

Trying to add text to the beginning of each line

Well here goes: I tried to write a batch file that adds a specific fixed text to each line of an already existing text file. for the adding text infront of each line I tried this: for /F "delims=" %%j in (list.txt) do echo.STARTTEXT\%%j >> list.txt for adding text after each line I... (6 Replies)
Discussion started by: pasc
6 Replies

3. Shell Programming and Scripting

Insert output from a file to beginning of line with sed

Hi I've been trying to search but couldn't quite get the answer I was looking for. I have a a file that's like this Time, 9/1/12 0:00, 1033 0:10, 1044 ... 23:50, 1050 How do I make it so the file will be like this? 9/1/12, 0:00, 1033 9/1/12, 0:10, 1044 ... 9/1/12, 23:50, 1050 I... (4 Replies)
Discussion started by: diesel88
4 Replies

4. Shell Programming and Scripting

SED - insert space at the beginning of line and multi replace command

hi I am trying to use SED to replace the line matching a pattern using the command sed 'pattern c\ new line ' <file1 >file 2 I got two questions 1. how do I insert a blank space at the beginning of new line? 2. how do I use this command to execute multiple command using the -e... (5 Replies)
Discussion started by: piynik
5 Replies

5. Shell Programming and Scripting

Need to insert text(constant) at the beginning of file

I am very new to scripting and I know this request is simple but I am having no luck with it. I have a file a.dat with the following data in it. aa bb cc dd I need to run a script that will take each line of a.dat and put dsjc/ubin/ in front of each record, so the output looks like ... (2 Replies)
Discussion started by: jclanc8
2 Replies

6. Shell Programming and Scripting

add a number to the beginning of every line

hey, i would like to add a line number to the beginning like so: red blue green yellow will be: 1=>red 2=>blue 3=>green 4=>yellowplease advise thank u. (5 Replies)
Discussion started by: boaz733
5 Replies

7. UNIX for Dummies Questions & Answers

Count Number Of lines in text files and append values to beginning of file

Hello, I have 50 text files in a directory called "AllFiles" I want to make a program that will go inside of the "AllFiles" Directory and count the number of lines in each individual text file. Then, the program will calculate how many more lines there are over 400 in each text file and... (7 Replies)
Discussion started by: motoxeryz125
7 Replies

8. Shell Programming and Scripting

Insert text at line number

I wrote a script to grep for a closing XML node. Then I need it to navigate up a line and insert some XML. Then go to the next occurrance. I have this INSERT_NODE='<QUANTITATIVE NAME="'${QR_NAME}'" QUANT="1" />' GREP_FOR='</JOB>' TMP_FILE=/tmp/lineArray.$$ if ]; then continue else ... (7 Replies)
Discussion started by: J-Man
7 Replies

9. Shell Programming and Scripting

Insert two strings at the beginning and at the end of each line of a file

Hi, excuse me for my poor english. My problem is that: I have a File i want to add to each line of that file two strings: one at the beginning of the line, one at the ending. string1="abcd" string2="efgh" i want $string1 content $string2 for each line. Is that possible? (3 Replies)
Discussion started by: Linux-fueled
3 Replies

10. Shell Programming and Scripting

Unix Script with line number at beginning of each line.

Could anybody help me. I need to create a script that reads a text file from STDIN and prints out the file to STDOUT with line numbers at the beginning of each line. Thanks. (5 Replies)
Discussion started by: mascorro
5 Replies
Login or Register to Ask a Question