How to assign value to a variable with row(using -n) returned by sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to assign value to a variable with row(using -n) returned by sed
# 1  
Old 08-25-2008
How to assign value to a variable with row(using -n) returned by sed

Hi Friends,

REQUIREMENT: Want to delete files from the current directory match with the same in the file test.txt

set -x
i=1
echo "i=$i"
COUNT=`sed -n '$=' test.txt`
echo "Count=$COUNT"
while [ $i -lt $COUNT ]
do


"## Here is error##"
FILETOREMOVE=`sed -n \'$i,1p\' test.txt`


echo $FILETOREMOVE
rm -f $FILETOREMOVE
i=`expr $i + 1`
done

Please let me know what is right way to assign value to a variable with rows(using -n) returned by sed?
# 2  
Old 08-25-2008
It's not necessary you use sed, this should do the job:

Code:
while read file
do
  echo "$file"
#  rm -f "$file"
done  < test.txt

Try it first with echo to be sure you get the right output.

Regards
# 3  
Old 08-25-2008
Hi Franklin,

Thank you for the new concept. I will sure use that in future....

However I will still love to know how to assign the values to variable in returned the the form of row from sed as I mentioned in my thread.

Here assignment operator "` `" was not working . What should be the right syntax to do it ? It might be useful to know..


Thanks
# 4  
Old 08-25-2008
Quote:
Originally Posted by sourabhsharma
Hi Friends,

REQUIREMENT: Want to delete files from the current directory match with the same in the file test.txt

set -x
i=1
echo "i=$i"
COUNT=`sed -n '$=' test.txt`
echo "Count=$COUNT"
while [ $i -lt $COUNT ]
do


"## Here is error##"
FILETOREMOVE=`sed -n \'$i,1p\' test.txt`


echo $FILETOREMOVE
rm -f $FILETOREMOVE
i=`expr $i + 1`
done

Please let me know what is right way to assign value to a variable with rows(using -n) returned by sed?
Replace single quotes by double quotes :
Code:
FILETOREMOVE=`sed -n "${i}p" test.txt`

You can use wc command instead of sed in the COUNT calcul :
Code:
count=`wc -l test.txt`

.

The best way to do the work is Franklin's solution.

Jean-Pierre.
# 5  
Old 08-25-2008
Tools Prefer to assign variables differently

Take a look at this example of variable assignment:
Code:
> ls file*
file1  file2  file3
> filecnt=$(ls file* | wc -l)
> echo $filecnt
3

Rather than the ` backticks, using the $(...) seems to be a little cleaner to execute and read. Perhaps modify your variable assignment similarly?
# 6  
Old 08-25-2008
Hi Aigles,

It is working Perfectly...

@Joeyg,

$(..) already had tried but didn't work...

But don't know why it worked with COUNT=`sed -n '$=' test.txt`.....

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

2. Shell Programming and Scripting

Regex in sed to find specific pattern and assign to variable

(5 Replies)
Discussion started by: radioactive9
5 Replies

3. Shell Programming and Scripting

Trim sed output & assign to variable

Hi, I have the following command that parses an xml file to read a node <port>'s value. Hoever the output comes with spaces. My requirement is to trim the spaces around the value and assign to a variable. sed -n 's|<port>\(.*\)</port>|\1|p' ../cfg.xml How do I go about it? (6 Replies)
Discussion started by: sai2013
6 Replies

4. Shell Programming and Scripting

check variable value when nothing is returned

Hi all, I am wondering how can I check when a variable has nothing returned in it. I am trying to store a pid in this variable to see if a script is running in the background. So I am using something like that proc_pid=`ps -ef |grep script.sh|grep -v grep|awk '{print $2}'` if then ... (1 Reply)
Discussion started by: geovas
1 Replies

5. Shell Programming and Scripting

Sed - using value returned by W+

Hi, In a file old.txt containing (for example) a series.. #limerick There was a young lady from Nantucket.. \images\Blank.jpg :end #joke A horse walked into a bar... \images\Blank.jpg end I would like to achieve new.txt containing #limerick There was a young lady from Nantucket..... (5 Replies)
Discussion started by: Nigel_R
5 Replies

6. Shell Programming and Scripting

Variable not found error for a variable which is returned from stored procedure

can anyone please help me with this: i have written a shell script and a stored procedure which has one OUT parameter. now i want to use that out parameter as an input to the unix script but i am getting an error as variable not found. below are the unix scripts and stored procedure... ... (4 Replies)
Discussion started by: swap21783
4 Replies

7. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

8. UNIX for Dummies Questions & Answers

Assigning the output of a command to a variable, where there may be >1 line returned?

Hello I am using unix CLI commands for the Synergy CM software. The command basically searches for a folder ID and returns the names of the projects the folder sits in. The result is assigned to a variable: FIND_USE=`ccm folder -fu -u -f "%name"-"%version" ${FOLDER_ID}` When the command... (6 Replies)
Discussion started by: Glyn_Mo
6 Replies

9. Shell Programming and Scripting

assign subst|grep|sed command result to a variable

Hi, I'm quite new to scripting and I want to modify following line of an existing script: MYVAR=`subst |grep 'L:\\\:' | sed -e 's/.*\\\//'`; What I have to do is to use the content of a variable instead of the constant expression 'L:\\\:' as the grep string to be matched. Assuming I already... (5 Replies)
Discussion started by: snowbiker99
5 Replies

10. Shell Programming and Scripting

Assign the returned value of a function to a variable

Hi, Can anyone please show me how to assign the returned value of a function to a variable? Thanks. (2 Replies)
Discussion started by: trivektor
2 Replies
Login or Register to Ask a Question