Can someone explain the 2 lines of code?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can someone explain the 2 lines of code?
# 1  
Old 03-19-2010
Can someone explain the 2 lines of code?

I have something like in the script. Can anyone explain what does the sort command does and followed by the sed

Code:
sort -r -k3.7,3 -k3.1,3 -k3.4,3 -t: $dir"abc_"$x"_parsed" | sed -e 's/\([0-9]\)\([0-9]\{3\}[^0-9]\)/\1,\2/2' -e 's/\/2,00/\/200/'

The file abc_"$x"_parsed contains something like below

Code:
010031781045376050468:617.96:03/17/2010:EVERGREEN_PACKAG:22_319D162084____4:****:****
010031781045376050489:260.60:03/17/2010:TW_TELECOM______:772206T626477397__:****:****


Last edited by pludi; 03-19-2010 at 10:57 AM..
# 2  
Old 03-19-2010
Well, it seems like the sort command reverse sorts your file on year, month, date, considering ":" as the delimiter, and then the sed command, working on each line, checks for the 2nd occurrence of the pattern that consists of a digit "D" followed by a string "s" that has 3 digits followed by a non-digit, and converts it to "D,s"; and finally, the sed changes the first occurrence of "/2,00" to "/200".

tyler_durden
# 3  
Old 03-19-2010
Quote:
Originally Posted by Muthuraj K
Code:
sort -r -k3.7,3 -k3.1,3 -k3.4,3 -t: $dir"abc_"$x"_parsed" | sed -e 's/\([0-9]\)\([0-9]\{3\}[^0-9]\)/\1,\2/2' -e 's/\/2,00/\/200/'


Whoever wrote it has the filename quoting backwards: the parts that don't need quoting are quoted, and those that need it are not.

Presumably the quotes are there to separate the variable name from adjacent text. For that, braces should be used:
Code:
"${dir}abc_${x}_parsed"

# 4  
Old 03-22-2010
Thanks for your explanations.

Would it be a problem, If I remove the sed from my script. Basically I get a
comma character in my year field If i have this.

I get 2,010 wherein I need 2010.

Can I remove the sed stuff below

---------- Post updated 03-22-10 at 08:00 AM ---------- Previous update was 03-21-10 at 11:58 PM ----------

Can anyone suggest what should I changing in the sed command so that I change only "/2,0" to "/20" instead of "/2,00" to "/200"

I need only the 2 characters of the year fied..
# 5  
Old 03-22-2010
So the "sed" statement should be like this:

Code:
 
sed -e 's/\([0-9]\)\([0-9]\{3\}[^0-9]\)/\1,\2/2' -e 's/\/2,0/\/20/'

Not sure , whether this is what your expecting.
# 6  
Old 03-22-2010
Yeah this is what I meant. Will this work without any implications for the year

Thanks for your inputs
# 7  
Old 03-22-2010
Quote:
Originally Posted by Muthuraj K
Yeah this is what I meant. Will this work without any implications for the year

...
If the year value begins with "20" always, and there's no occurrence of "/2,0" before it, then the year will remain unchanged. In fact, the sed script is not required at all in that case, since the two scripts after "-e" would cancel out each other.
Otherwise, if the year value begins with something other than "20", then it will have the comma after the first digit, unless there's an occurrence of "/2,0" before it.

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

can you explain this sed code?

can anyone please explain this code? sed ':a;N;$!ba;s/]\n//g' file it replaces lines ending with "]" and concatenates with the next line so that line1] line2 becomes line1line2 i don't understand this part: :a;N;$!ba; I have noted that I can replace "a" with any letter: ... (1 Reply)
Discussion started by: locoroco
1 Replies

2. Shell Programming and Scripting

Please explain what this Awk code is doing

Hi Guys, Please help me, I am new to programming and I don’t understand what some parts of this code are doing. I have comments on the parts I know, please help if my understanding of the code is not correct and also help with parts with questions. awk ' { gsub( ">",... (1 Reply)
Discussion started by: James_Owen
1 Replies

3. Shell Programming and Scripting

Please explain what this code is doing

Hi, Pls explain me what the below code is doing. specially meaning if -a while calling test function- case $1 in 1) beg_dt=01; end_dt=07 ;; 2) beg_dt=08; end_dt=14 ;; 3) beg_dt=15; end_dt=21 ;; 4) beg_dt=22; end_dt=28 ;; 5) beg_dt=29; end_dt=31 ;; esac test \( `date +%w` -eq $2 -a... (3 Replies)
Discussion started by: sendtoshailesh
3 Replies

4. Shell Programming and Scripting

Explain SED code

Hi, Can anyone pls explain me the below SED code in detail. sed -e :a -e '$!N;s/\n//;ta' -e P -e D When this code is executed with a file(has 1lac records), it is taking very long time to process. So I wanted to modify this SED code with equivalant AWK code. Thanks, Sri (1 Reply)
Discussion started by: srilaxmi
1 Replies

5. Programming

Explain a block of code:

Hi, I have this block in a code and I need to know it's meaning: bool selectEvents = config.get("selectEvents",false); if (selectEvents) { ifstream in("events"); while (in) { int run, evt; in >> run >> evt; if (in.eof()) break; ... (5 Replies)
Discussion started by: eager2no
5 Replies

6. Shell Programming and Scripting

Please explain what code is doing between the two dotted lines

Can someone please explain the meaning of code between the two lines below. if ; then echo "./filter.sh <a flat file containing group and id>" fi ------------------------------------------------------------ >IDnotexist #account in request does not exist in NIS... (1 Reply)
Discussion started by: pinnacle
1 Replies

7. Shell Programming and Scripting

can any one explain the meaning of the below code ?

while getopts :g:l:h: KEY $* do case $KEY in g) NAME=$OPTARG ;; l) LAB=$OPTARG ;; h) HD=$OPTARG ;; *) echo "Illegal option: $OPTARG" echo "$usage" exitStatus=1 exitGrace esac done (2 Replies)
Discussion started by: santosh1234
2 Replies

8. Shell Programming and Scripting

Can any one explain what this code will do

ccc_con,CCC_CON,0 Above is the input for this code #!/usr/bin/bash my_path=`dirname $0` T_table=$1 S_table=$2 P_table=$3 #Star new code while read ${my_path}/arch_table_list.txt { awk -F "," '{print $1}' ${my_path}/arch_table_list.txt ${S_table} awk -F "," '{print... (1 Reply)
Discussion started by: scorp_rahul23
1 Replies

9. UNIX for Advanced & Expert Users

explain the code

Hi , Can anyone explains what does the below highlighted statements means: # Set environment variables . ${0%/*}/wrkenv.sh jobName_sh=${0##*/} jobName=${jobName_sh%.*} Thanks, Sri (1 Reply)
Discussion started by: srilaxmi
1 Replies

10. UNIX for Dummies Questions & Answers

could someone explain this code

hey peeps could someone explain what this part of the code means: 'if echo $* | grep -q' $i '|| thanks tHe_nEw_GuY (2 Replies)
Discussion started by: the_new_guy
2 Replies
Login or Register to Ask a Question