sed command to get the total lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed command to get the total lines
# 1  
Old 07-31-2008
sed command to get the total lines

hi,
I am using the following code to get the first field of all the lines in a file using sed command.But its not printing the last line.Why it is so and how can i read the different fields of each line ?

code:
for i in $(sed = filename.txt | sed 'N;s/\n/ /' | cut -d " " -f1)
do
val1=`sed -n "${i},${i}p" filename.txt | cut -d " " -f1`
echo "$val1"
done
# 2  
Old 07-31-2008
Hi,

mychk.txt
check for 1 2
for 2 also 4
form 3 also 5 6

ksr/Scripts> cat mychk.txt | cut -d' ' -f1-3
check for 1
for 2 also
form 3 also
ksr/Scripts> cat mychk.txt
check for 1 2
for 2 also 4
form 3 also 5 6
ksr/Scripts> cat mychk.txt | cut -d' ' -f1-3
check for 1
for 2 also
form 3 also
ksr/Scripts> cat mychk.txt | cut -d' ' -f1,3
check 1
for also
form also
ksr/Scripts> cat mychk.txt | cut -d' ' -f1,3-4
check 1 2
for also 4
form also 5
# 3  
Old 07-31-2008
Whoa, that's incredibly complex. What's wrong with just cut -d " " -f1 filename.txt?

You don't need sed to enumerate the line numbers in the for loop. Just nl filename.txt would get you file numbering (with some option to nl to also number blank lines -- I seem to recall -ba).

But why do you need the line numbers in the first place? And why do you grab the result into a variable and then echo that variable, when simply sed -n ${i}p filename.txt | cut -d " " -f1 without the backticks and variable and echo would print what you want?
# 4  
Old 07-31-2008
I need to retrieve more than 1 field in all the lines.
code:
for i in $(sed = filename.txt | sed 'N;s/\n/ /' | cut -d " " -f1)
do
val1=`sed -n "${i},${i}p" filename.txt | cut -d " " -f1`
val2=`sed -n "${i},${i}p" filename.txt | cut -d " " -f3`
val3=`sed -n "${i},${i}p" filename.txt | cut -d " " -f4`
printf "%s\t%s\t%s\n" "$val1" "$val2" "$val3"
done
# 5  
Old 07-31-2008
How about this instead?

Code:
while read first second third forth rest; do
  printf "%s\t%s\t%s\n" "$first1" "$third" "$forth"
done <filename.txt

# 6  
Old 07-31-2008
its working fine.thanks
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 condense two lines into one

My eyes glaze over trying to understand multiline sed commands, so please forgive me if this has been asked before. I'd like to condense the following two lines: fooPATTERN1 PATTERN2bar into the following one line: foobar How is that done in sed? (7 Replies)
Discussion started by: scolfax
7 Replies

2. Shell Programming and Scripting

sed command removes lines in file

Hi, I am modifying a file with sed command. i want to make SCORE= blank in the file whereever SCORE=somevalue. What will *$ do in the below command? cat $file | sed 's/SCORE=.*$/SCORE=\r/g' > newfile The last line is also missing in the new file. How to make SCORE='100' to SCORE=... (5 Replies)
Discussion started by: ashok.k
5 Replies

3. Shell Programming and Scripting

Select lines in which column have value greater than some percent of total file lines

i have a file in following format 1 32 3 4 6 4 4 45 1 45 4 61 54 66 4 5 65 51 56 65 1 12 32 85 now here the total number of lines are 8(they vary each time) Now i want to select only those lines in which the values... (6 Replies)
Discussion started by: vaibhavkorde
6 Replies

4. Shell Programming and Scripting

sed command : print lines having no # at the begining

I am trying to print those line which has no # in the begining of the line. The sed I used for this purpose as shown below is not giving the required output. echo 'PDE 5600' | sed -n 's/^\!#/&/p' Where lies the problem:confused: (3 Replies)
Discussion started by: hiten.r.chauhan
3 Replies

5. Shell Programming and Scripting

What's the command to remove empty lines with sed?

3 10 20 10 100 100 10000 Output: 3 10 20 10 100 100 10000 ---------- Post updated at 07:59 AM ---------- Previous update was at 07:56 AM ---------- sed '/^$/d' file doesn't work. (8 Replies)
Discussion started by: cola
8 Replies

6. Solaris

sed command to print lines after expression

Hi guys. I need a sed command to print like 10 lines after a regular expression is found in the log. Can anyone help me out. Thanks ---------- Post updated at 10:52 AM ---------- Previous update was at 10:34 AM ---------- never mind. I just did the search bewteen two expressions. (1 Reply)
Discussion started by: jamie_collins
1 Replies

7. Shell Programming and Scripting

How to use sed command for change special lines?

Hi, I want to add a character "#" in this few lines with sed command Initial: ### CACCIA: DEBUT ### if $(grep -wqi "$2" /etc/passwd); then chuser -R files registry=files $2 fi ### CACCIA: FIN ### Result with sed command: ### CACCIA: DEBUT ### #if $(grep -wqi "$2"... (4 Replies)
Discussion started by: khalidou13
4 Replies

8. UNIX for Dummies Questions & Answers

Sed command over multiple lines in csh??

hi i have a long sed command in a csh script that won't fit on 1 line. how do i break it up correctly over multiple lines? this doesn't seem to work in csh: sed -e s/template/$IP.$NN/ \ -e s/NRG/6/ \ -e s/inputf/$IS.$NN/ \ -e s/SHIFT/10.0/ <template.egsinp > $IP.$NN.inp i get: sed:... (1 Reply)
Discussion started by: tuathan
1 Replies

9. Shell Programming and Scripting

awk/sed Command : Parse parameter file / send the lines to the ksh export command

Sorry for the duplicate thread this one is similar to the one in https://www.unix.com/shell-programming-scripting/88132-awk-sed-script-read-values-parameter-files.html#post302255121 Since there were no responses on the parent thread since it got resolved partially i thought to open the new... (4 Replies)
Discussion started by: rajan_san
4 Replies

10. Shell Programming and Scripting

using sed command to replace multiple lines

the file contains the follwoing lines /* * Copyright (C) 1995-1996 by XXX Corporation. This program * contains proprietary and confidential information. All rights reserved * except as may be permitted by prior written consent. * * $Id: xxx_err.h,v 1.10 2001/07/26 18:48:34 zzzz $ ... (1 Reply)
Discussion started by: radha.kalivar
1 Replies
Login or Register to Ask a Question