Passing a variable to sed or cut


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing a variable to sed or cut
# 1  
Old 08-08-2008
Passing a variable to sed or cut

Smilie Is it possible to send a variable to a sed or cut command? I have a test script as below:

counter=1
while read line
do
# Test the file
printf "$line" > temp$counter
pref=$(cut c1-2000 $temp$counter | sed 's/[^a-zA-Z0-9+_:-]//g' | sed 's|.*PutTime\(.*)Origin.*|\1|')
printf" let counter=counter+1
done < temp01

What I would like is to avoid writing the $line to a file and the passing it to the pref=....... line. What I would like to do is say assign the contents of the $line to a variable, say, record and then use record in the cut/sed command. I can change the first sed with cut i.e. cut after sed instead of doing it first.
# 2  
Old 08-08-2008
Quote:
Originally Posted by gugs
Smilie Is it possible to send a variable to a sed or cut command? I have a test script as below:

counter=1
while read line
do
# Test the file
printf "$line" > temp$counter
pref=$(cut c1-2000 $temp$counter | sed 's/[^a-zA-Z0-9+_:-]//g' | sed 's|.*PutTime\(.*)Origin.*|\1|')
printf" let counter=counter+1
done < temp01

What I would like is to avoid writing the $line to a file and the passing it to the pref=....... line. What I would like to do is say assign the contents of the $line to a variable, say, record and then use record in the cut/sed command. I can change the first sed with cut i.e. cut after sed instead of doing it first.
Code:
counter=1
while read line
do
# Test the file 
pref=$(echo $line | cut c1-2000 | sed 's/[^a-zA-Z0-9+_:-]//g' | sed 's|.*PutTime\(.*)Origin.*|\1|')
printf" let counter=counter+1
done < temp01

# 3  
Old 08-08-2008
Just put pref=$(printf "$line" | cut ...)

However, be advised that this sounds like the tail wagging the dog. Reading a line at a time in a while loop and feeding it through a complex pipeline of shell commands with echo is rarely a good solution, and often a sign that you should approach the problem with a few lines of awk or sed which you run directly on the input file.
# 4  
Old 08-08-2008
The echo command does not work, can use printf because

When I enter the *echo $line* | cut ...... I get a messages displayed saying *echo: command not found.

Any ideas?



I cant use print command because it doesn't like some characters within the data.
# 5  
Old 08-08-2008
Era how?

Era, How could I use awk or sed directly on the input file?

Any examples would be welcome
# 6  
Old 08-11-2008
I don't see any *echo, did you mistype it?

Code:
awk '{ f=substr($0, 1, 2000); gsub (/[^a-zA-Z0-9+_:-]/, "", f);
    gsub (/^.*PutTime/, "", f); gsub (/Origin.*$/, "", f); print f }' temp01

This is rather unattractive but should hopefully give you an idea of how to proceed. In particular, the repeated gsubs and the substring extraction could probably be optimized if we knew more about the input data format.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Passing value of a variable in sed

Hi, I want to pass value of a variable track_line which is the line number to sed. Sed should print the lines starting from track_line till the last line of the file. I tried the below command but it is not working. sed -n '${track_line},$p' latest_log_file I tried using the below too but... (1 Reply)
Discussion started by: nitinupadhyaya8
1 Replies

2. Shell Programming and Scripting

sed and cut command in variable

hi, i want to remove 12 and 13 column from psv files and dump them in new folder ls -ltr *GTDA_Dly_Pmix_*.psv>filename.xls var1=`cat filename.xls` for i in $var1 do var3=`echo "$i" |cut -d '|' -f12,13 |sort -u` sed -e 's/"|$var3"//g... (2 Replies)
Discussion started by: renuk
2 Replies

3. Shell Programming and Scripting

Trouble with passing variable to sed

Here is my code #!/bin/bash username=gnowicki sed '$s/$/ $username/' < sshd_config 1 <> sshd_config what this is supposed to do is take the name gnowicki and put it at the end of the last line of the sshd_config and it works except not using the variable, if I put the name "gnowicki" where... (2 Replies)
Discussion started by: slufoot80
2 Replies

4. Shell Programming and Scripting

passing variable to sed command not working

Hello All, I am trying to embed variable in sed command to fetch a portion of record between two pattern. This command is not working ...any suggestion on this how to place the variable in sed command to find a portion . I am using Sun OS (Solaris). Thanks JM (1 Reply)
Discussion started by: jambesh
1 Replies

5. Shell Programming and Scripting

Passing a variable to sed command

Hi guys, I wanted to pass a variable to the sed command which tells which line to be deleted. a=2; echo $a; sed '$ad' c.out it is throwing an error. sed: 0602-403 "$a"d is not a recognized function. I even tried "$a" and \$a.. but it is of no use. Can you please correct me... (6 Replies)
Discussion started by: mac4rfree
6 Replies

6. Shell Programming and Scripting

Passing Variable in sed

Dear All, I want to print a file. First I tried with this sed '2q;d' filename it worked. But when i put following it is not working x=2; sed '$xq;d' filename Would any one suggest how to pass the variable? (7 Replies)
Discussion started by: saifurshaon
7 Replies

7. Shell Programming and Scripting

Problem passing a specific variable to sed

Hi, I'm a bit of sed n00b here. My issue is as follows: I'm trying to pass a variable to sed so that all instances of this variable (in a text file) will be replaced with nothing. However, the value of this variable will always be a folder location e.g. "C:\Program Files\Folder1" I... (5 Replies)
Discussion started by: Mr_Plow
5 Replies

8. Shell Programming and Scripting

Passing a variable to sudo sed command

hi, dataParse(){ line="$@" name="cat /etc/passwd | grep "$line": | cut -f6 -d':'" eval $name > sam.txt 2>&1 sudo -u $line sed -n 's/data-1/&/p' $name/test.xml >> sam1.txt } Here i getting the homedir of the accounts and is set in name variable.which returns "/home/raju" which i... (3 Replies)
Discussion started by: sachin.tendulka
3 Replies

9. Shell Programming and Scripting

Passing a variable in SED getting command garbled

I fairly new to SED. I have tried many different variations of this line of code and even breaking it down into its components and running them separately. They work individually without variables but when I place the $todbname variable it will either inserts the text "connect to $todbname"... (3 Replies)
Discussion started by: edewerth
3 Replies

10. Shell Programming and Scripting

variable passing to sed

I m trying to pass variable to sed. export var=140920060731 sed -e '/$var/d' file but no luch so far..? any body has any idea abt it Is there any way to pass variable to SED? Thanks , Manish (2 Replies)
Discussion started by: Manish Jha
2 Replies
Login or Register to Ask a Question