Line number in sed


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Line number in sed
# 1  
Old 05-13-2014
Line number in sed

Hi,

I have txt file like below

Code:
02.05.2014,apple,homeworbook,red pen,yellow
10.05.2014,cookies,language book,water pain,red
10.05.2014,carot,science,purple,green

I have written below code

Code:
#!/bin/ksh
echo "enter week"
read week
sed -n '"$week"' inputfile >>file

if I enter
Code:
week 1

the first row has to print in file
# 2  
Old 05-13-2014
Hello Stew,

Could you please try the following code.

Code:
$ cat check_count_first_row_check121.ksh
echo "enter week"
read week
echo ""
awk -vs1="$week" 'BEGIN{print s1} 1' check_count_first_row_check121

Output will as follows.

Code:
$ ksh check_count_first_row_check121.ksh
enter week
week1
 
week1
02.05.2014,apple,homeworbook,red pen,yellow
10.05.2014,cookies,language book,water pain,red
10.05.2014,carot,science,purple,green

Note: Where check_count_first_row_check121.ksh is the script name and check_count_first_row_check121 is the input file name.


Thanks,
R. Singh
# 3  
Old 05-13-2014
Assuming that the whatever follows "week" is the line number, try:
Code:
#!/bin/ksh
 echo "enter week" 
read week 
line=$(echo $week | awk '$1 ~ /week/ {print $2}')
[ ! -z $line ] && sed -n "$line p" inputfile >>file

# 4  
Old 05-13-2014
Thanks for all your response. I fixed it, below is my code

Code:
#!/bin/ksh
echo "enter week"
read week
sed -n "$week p" inputfile>>file

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

2. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

3. Shell Programming and Scripting

Sed print range of lines between line number and pattern

Hi, I have a file as below This is the line one This is the line two <\XMLTAG> This is the line three This is the line four <\XMLTAG> Output of the SED command need to be as below. This is the line one This is the line two <\XMLTAG> Please do the need to needful to... (4 Replies)
Discussion started by: RMN
4 Replies

4. Red Hat

pass a variable line number to sed

num=10 sed -n '$num p' test.txt sed -n '10 p' test.txt works however i am putting the sed command in a loop and the line number is not static Can someone please help me how to achive this. (1 Reply)
Discussion started by: figure20012
1 Replies

5. Shell Programming and Scripting

Help on Sed/awk/getting line number from file

I Have file1 with below lines : #HostNameSelection=0 :NotUsed #HostNameSelection=1 :Automatic #HostNameSelection=3 :NotForced I have file2 which has similar lines but with different values I want to copy the changes from file1 to file2 ,line by line only if line begins with '#'. for... (7 Replies)
Discussion started by: mvr
7 Replies

6. Shell Programming and Scripting

sed script - print the line number along with the line

Hi, I want to print the line number with the pattern of the line on a same line using multi-patterns in sed. But i don't know how to do it. For example, I have a file abc def ghi I want to print 1 abc 2 def 3 ghi I know how to write it one line code, but i don't know how to put... (11 Replies)
Discussion started by: ntpntp
11 Replies

7. UNIX for Dummies Questions & Answers

Using awk to get a line number to delete, piping through sed

Alright, I'm sure there's a more efficient way to do this... I'm not an expert by any means. What I'm trying to do is search a file for lines that match the two input words (first name, last name) in order to remove that line. The removal part is what I'm struggling with. Here is my code: echo... (4 Replies)
Discussion started by: lazypeterson
4 Replies

8. Shell Programming and Scripting

sed/awk to insert comment at defined line number

Hi there, may someone easily help me on this : I want to insert a text in a specific line number like : linenumb2start=`cat memory_map.dld | nl -ba | egrep -i "label" | cut -f1` line2insert=`expr $linenumb2start + 2` and now I need to replace something like {} with {comment} at... (8 Replies)
Discussion started by: homefp
8 Replies

9. Shell Programming and Scripting

SED: Update Last Line with Number Lines in File

Hi, I have to update last line of a text file with the number of lines in that file. This last line will have text such as 0.0000 and I should replace this with number lines. If lines are 20 then it should be replaced with 00020. Any sed or awk cmd help would be appreciated (3 Replies)
Discussion started by: bmkux
3 Replies

10. Shell Programming and Scripting

Number a list at end of line using 'sed'

Hi All I have a script which has produced a list, I have used 'sed' to number my list, but i want to list at end of line with the first line starting at zero (0) and brackets round it ie My List i want Hello (0) this (1) day (2) can (3) be (4) sed '/./=' filename | sed '/./N; s/\n/) /'... (5 Replies)
Discussion started by: chassis
5 Replies
Login or Register to Ask a Question