Passing UNIX commands inside sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing UNIX commands inside sed
# 8  
Old 03-01-2009
Thanks to everyone for your replies. I guess I owe a better explanation on what I'm trying to do.

I have a file that has a header, trailer and some detail records.
e.g. file1.txt

HEADER|value1|value2|value3
12345|value1|value2|value3
12345|value1|value2|value3
6789|value1|value2|value3
TRAILER|5|value2

where the value 5 in the TRAILER record is the record count of the file

My objective is to move the header,trailer and detail records beginning with 12345 to a new file and replace the record count on the new file. Given below is the code I have and it managed to do what I wanted (There might be some syntax issues since I'm on my windows PC and don't have access to the actual code I had written on my unix machine)

cat file1.txt | egrep -i "^[header|trailer|12345]" > tmp.txt # to move the desired records from file1.txt to tmp.txt

newcount=`cat tmp.txt | wc -l` # get the record count of tmp.txt

cat tmp.txt | egrep -i "^trailer" | cut -d "|" -f2 | ## get the second field from the trailer record in tmp.txt
while read count
do
sed "s/$count/$newcount/" > tmp1.txt
mv tmp1.txt file2.txt
done


My expected output on file2.txt is

HEADER|value1|value2|value3
12345|value1|value2|value3
12345|value1|value2|value3
TRAILER|4|value2

Instead of the WHILE loop, I wanted to know if we can directly use sed as I had intended in my original question to achieve the same result.

So, will something like

cat tmp.txt | sed "/^TRAILER/s/`cut -d "|" -f2`/$newcount/" > file2.txt

or
cat tmp.txt | egrep -i "^trailer" | sed "/s/`cut -d "|" -f2`/$newcount/" > file2.txt

work?



-Thanks
# 9  
Old 03-01-2009
You can certainly get the results you want using cat, cut, egrep, sed, etc. but that method is compute intensive becauses of the number of forks, execs, etc. You can easily do what you want to do entirely within any modern shell.

For example the following works for both bash, and ksh93
Code:
count=1
IFS="|"
while read prefix value line
do
   if (( count == 1 )) || [[ $prefix = "12345" ]]
   then
      echo "$prefix|$value|$line"
      (( count++ ))
   fi
   if [[ $prefix = "TRAILER" ]]
   then
      echo "$prefix|$count|$line"
   fi
done < file1.txt

# 10  
Old 03-01-2009
With awk:

Code:
awk -F"|" '
NR==1 || $1=="12345"{print;c++}
/TRAILER/{$2=++c;print}
' OFS="|" file1.txt

This is the output:

Code:
$ cat file1.txt
HEADER|value1|value2|value3
12345|value1|value2|value3
12345|value1|value2|value3
6789|value1|value2|value3
TRAILER|5|value2
$
$
$ awk -F"|" '
NR==1 || $1=="12345"{print;c++}
/TRAILER/{$2=++c;print}
' OFS="|" file1.txt
HEADER|value1|value2|value3
12345|value1|value2|value3
12345|value1|value2|value3
TRAILER|4|value2
$
$

Regards
# 11  
Old 03-01-2009
Quote:
Originally Posted by fpmurphy
You can certainly get the results you want using cat, cut, egrep, sed, etc. but that method is compute intensive becauses of the number of forks, execs, etc. You can easily do what you want to do entirely within any modern shell.

But your script will not work in "any modern shell". You use a number of non-standard constructs all of which can be replaced by standard syntax with no loss of efficiency.
Quote:
For example the following works for both bash, and ksh93
Code:
count=1
IFS="|"
while read prefix value line
do
   if (( count == 1 )) || [[ $prefix = "12345" ]]
   then
      echo "$prefix|$value|$line"
      (( count++ ))
   fi
   if [[ $prefix = "TRAILER" ]]
   then
      echo "$prefix|$count|$line"
   fi
done < file1.txt


Translated to work in any standard UNIX shell:

Code:
count=1
while IFS="|" read prefix value line
do
   if [ $count = 1 ] || [ $prefix = "12345" ]
   then
      echo "$prefix|$value|$line"
      count=$(( $count + 1 ))
   fi
   if [ "$prefix" = "TRAILER" ]
   then
      echo "$prefix|$count|$line"
   fi
done < file1.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Passing parameter inside the expression

Hello, i need to pass the variable in place of pwd. how to display variable in the bleow syntax. suppose, passwd="test", then 'id/$passwd..... FEDFlagResult=`sqlplus -S 'id/pwd@(DESCRIPTION= (ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1643))(CONNECT_DATA= (SID=peta1)))'<< EOF select... (4 Replies)
Discussion started by: balareddy
4 Replies

2. Shell Programming and Scripting

Trouble passing commands with expect

Hello All, I hope someone could help me with this. I'm creating a shell script to run a process. The trouble is, part of the process has to be ran as a different user. I can 'su' to the user ok, but I'm having trouble passing a 'cd' command as well as some variables I set earlier in the... (1 Reply)
Discussion started by: bbbngowc
1 Replies

3. Shell Programming and Scripting

Problem passing a search pattern to AWK inside a script loop

Learning, stumbling! My progress in shell scripting is slow. Now I have this doubt: I have the following file (users.txt): AU0909,on AU0309,off AU0209,on AU0109,off And this file (userson.txt) AU0909 AU0209 AU0109 AU0309 I just want to set those users on userson.txt to "off" in... (14 Replies)
Discussion started by: quinestor
14 Replies

4. Shell Programming and Scripting

passing arguments to unix command or script inside tclsh

hi everobody kindly consider the following in tclsh I understand that we can do the following %exec UnixCmd arg1 arg2 but if I assinged the arguments to a list insde tclsh how can I use them back i.e %set ArgList %exec UnixCmd %exec Unixcmd $list %exec all the... (1 Reply)
Discussion started by: Blue_shadow
1 Replies

5. Shell Programming and Scripting

Passing parameter in sed or awk commands to print for the specific line in a file

Hi, I am trying to print a specific line in a file through sed or awk. The line number will be passed as a parameter from the previous step. My code looks as below. TEMP3=`sed -n '$TEMP2p' $FILEPATH/Log.txt` $TEMP2, I am getting from the previous step which is a numerical value(eg:3). ... (2 Replies)
Discussion started by: satyasrin82
2 Replies

6. Shell Programming and Scripting

Using commands inside sed substitution

Dear Friends, Please let me know how to use the date command inside the substitution flag replacement string. echo "01 Jan 2003:11:00:06 +0100" | sed 's/\(.*\)/`date -d \1 "+%Y%m%d%H%M%S"`/' I want to supply \1 string to Here mention below as part of replacement string, date -d <Here>... (5 Replies)
Discussion started by: tamil.pamaran
5 Replies

7. Shell Programming and Scripting

Passing unix command to "sed"

I am trying a simple sed to replace a pattern but it fails ,,c an someone help in where iam wrong, here is what i run and i get no o/p a=`cat abc.sh | grep -i pattern1 | egrep -iv "pattern2" ` sed 's/`echo $a`/XYZ=newvalue/g' < abc.sh > abc.sh_new This is how my ksh -x of teh script looks... (2 Replies)
Discussion started by: yesmani
2 Replies

8. Shell Programming and Scripting

Unix commands failing inside the shell script

When my script deals with large input files like 22Gb or 18 GB the basic commands like sort or join fails when run from inside the shell scripts. Can there be any specific reason for this? For e.g. sort -u -t "," -k1,1 a.csv > a.csv.uniq" sort -u -t "," -k1,1 b.csv > b.csv.uniq" The... (3 Replies)
Discussion started by: esha
3 Replies

9. Shell Programming and Scripting

How to run unix commands in a new shell inside a shell script?

Hi , I am having one situation in which I need to run some simple unix commands after doing "chroot" command in a shell script. Which in turn creates a new shell. So scenario is that - I need to have one shell script which is ran as a part of crontab - in this shell script I need to do a... (2 Replies)
Discussion started by: hkapil
2 Replies

10. Shell Programming and Scripting

passing variables to sed inside script

I am trying to pass a regular expression variable from a simple script to sed to remove entries from a text file e.g. a='aaaa bbbb cccc ...|...:' then executing sed from the script sed s'/"'$a"'//g <$FILE > $FILE"_"1 my output file is always the same as the input file !! any... (5 Replies)
Discussion started by: Daniel234
5 Replies
Login or Register to Ask a Question