sed command explanation needed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed command explanation needed
# 1  
Old 09-26-2006
sed command explanation needed

Hi,

Could you please explain me the below statement -- phrase wise.

sed -e :a -e '$q;N;'$cnt',$D;ba' abc.txt > xyz.txt


if suppose $cnt contains value: 10
it copies last 9 lines of abc.txt to xyz.txt

why it is copying last 9 rather than 10.
and also what is ba and $D over there in command.

reply me as early as possible.

Thanks,
Subbu
# 2  
Old 09-26-2006
A more lisible format of the command (with variable substitition made)
Code:
sed -e '
:a
$q
N
10,$D
ba
' abc.txt > xyz.txt

:a

(0):label Marks a branch point to be referenced by the b and t subcommands.
This label can be any sequence of eight or fewer bytes.

Defines label a

$q

(1)q Branches to the end of the script. It does not start a new cycle.

$ means last line of input.
If last line of input, print the line and stop processing.

N

(2)N Appends the next line of input to the pattern space with an embedded
new-line character (the current line number changes). You can use this to search for patterns that are split onto two lines.


10,$D

(2)D Deletes the initial segment of the pattern space through the first new-line character and then starts the next cycle.

From line 10 to end, removes the oldest line (added by N command) at the begining of the buffer.
So the buffer contains all time at most 9 lines.

ba

(2)b[label] Branches to the : command bearing the label variable. If the label variable is empty, it branches to the end of the script.

Branch to label a


The command is equivalant to :
Code:
tail -f 9 abc.txt > xyz.txt


Jean-Pierre.
# 3  
Old 09-26-2006
Hi Jean-Pierre,

Thank you very much for your quick response.

Is this correct way to copy all the lines of abc.txt, except the first line to xyz.txt using sed command as following....

cnt=`cat abc.txt | wc -l`
sed -e :a -e '$q;N;'$cnt',$D;ba' abc.txt > xyz.txt

or, is there any effective way of doing this using sed command?
Pls, give me solution using sed command only.

Thanks,
Subbu.
# 4  
Old 09-26-2006
The simplest manner, erases the first line and prints the others
Code:
sed -e '1d' abc.txt > xyz.txt

Why solution using sed command only ? Smilie


Jean-Pierre.
# 5  
Old 09-26-2006
Hi

Hi Jean-Pierre,

why I have asked you only sed command is .... I don't want to use tail command (it's truncating some part of data as it handles in only 20 k buffer). Let me know any other options available except tail.

Thank you very much,
Subbu.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Explanation for Scripts Inner Workings Needed

#!/bin/bash n=$l; typeset -a v x=$(< input.dat) check(){ if; then sed 's/Test/Proc/g' file.sh >fl.sh else exit 13 fi } check $n while ; do x=`expr $x -l` v=$x done less fi.sh l>/dev/null&& echo yes || exit 1 echo v= ${v } exit 0 I have file.sh and input.dat in the current... (3 Replies)
Discussion started by: bananasprite
3 Replies

2. Shell Programming and Scripting

Little explanation needed on array

I had gone through..google search.....and unix user post.......where I found so many ways of accessing files..... suppose if I am having 4 files, each file is having 3 columns, and I want to use each field of each column, then how can I use it.. how can I create array for each file's each column,... (8 Replies)
Discussion started by: Dona Clara
8 Replies

3. Shell Programming and Scripting

sed command explanation

Will someone give me an explanation on how the sed command below works. sed 's/.*//' Thanks! (3 Replies)
Discussion started by: scj2012
3 Replies

4. UNIX for Advanced & Expert Users

Grep - Explanation needed.

grep -E '^(++){5}5000' <file_name> this command searches value 5000 in only 6th column from provided file where pipe ( | )is delimiter which separate columns... can some one plz explain me what '^(++){5}5000' actually does..? :confused: (1 Reply)
Discussion started by: Killer420
1 Replies

5. Programming

Python 3.1 TypeError explanation needed

Could someone explain why Python 3.1 errors out below? Do I need an additional module that's not required in 3.2 perhaps? I need to use 3.1 as it's the version available on a server I am using. Python 3.2.1rc1 (default, May 18 2011, 11:01:17) on linux2 Type "help", "copyright", "credits"... (0 Replies)
Discussion started by: jelloir
0 Replies

6. Shell Programming and Scripting

sed sorting command explanation

sed '$!N; /^\(.*\)\n\1$/!P; D' i found this file which removes duplicates irrespective for sorted or unsorted file. keep first occurance and remove the further occurances. can any1 explain how this is working.. i need to remove duplicates following file. duplicate criteria is not the... (3 Replies)
Discussion started by: mukeshguliao
3 Replies

7. Shell Programming and Scripting

Explanation Needed

Hi all, I'm very new to UNIX. I have got a coding, where i dont understand the below part. Could someone please explain it in detail? awk 'NR > 1; NR == 1 { S = $0 } END { print S }' $textfile.bak > $textfile could someone explain what awk 'NR > 1; NR == 1 { S = $0 } END { print S }' ... (1 Reply)
Discussion started by: raghulshekar
1 Replies

8. UNIX for Dummies Questions & Answers

Explanation of the command sed -n -e 's_.*>\(.*\)<.*_\1_p' filename.xml

Hi Friends, I am trying to modify a script .The script contains this line: sed -n -e 's_.*>\(.*\)<.*_\1_p' filename.xml I am not great in sed command.I know, it is regular expression to match a pattern string that starts with s_ and ends with 1_.I doesnot give the desired result. Can... (4 Replies)
Discussion started by: rajsharma
4 Replies

9. UNIX for Dummies Questions & Answers

SED command explanation

can someone please explain the below sed command.. sed 's/\(*|\)\(.*\)/\2\1/' (6 Replies)
Discussion started by: raghu_shekar
6 Replies

10. UNIX for Dummies Questions & Answers

Exec explanation needed

Hello! I want to read a file line by line and have each line in a variable. I have found the following code. #!/bin/bash exec 3< data while read <&3 do echo "The number is $REPLY" a.out "$REPLY" done exec 3>&- I don't understand the use of exec and its arguments, though having read... (3 Replies)
Discussion started by: myle
3 Replies
Login or Register to Ask a Question