Computing.Net > Forums > Unix > Extract text using sed

Extract text using sed

Reply to Message Icon

Original Message
Name: ahteck
Date: June 16, 2003 at 21:38:57 Pacific
Subject: Extract text using sed
OS: AIX UNIX
CPU/Ram: 512
Comment:

How to use sed to extract the text

I have a text file look like this:

/home1/users/usr1/bill.txt
/home1/users/usr2/bom.txt
/tmp/cost.txt

All filename extension ended with txt.

I need to output the as follow:

/home1/users/usr1
/home1/users/usr2
/tmp

Any idea? Please help.



Report Offensive Message For Removal


Response Number 1
Name: nails
Date: June 16, 2003 at 23:22:27 Pacific
Subject: Extract text using sed
Reply: (edit)

Hi:

IMO, sed is not the best solution for this problem. Most unix variants supply the dirname command:

#!/bin/ksh

while read str
do
dirname $str
done xfile
# need a less than sign between done
# and xfile. This forum wipes it out

You can also use the korn shell pattern matching operator to perform the same function:

while read str
do
echo ${str%/*}
done xfile
# need a less than sign between done #and xfile. This forum wipes it out

Regards,

Nails


Report Offensive Follow Up For Removal

Response Number 2
Name: Jimbo
Date: June 17, 2003 at 00:34:51 Pacific
Subject: Extract text using sed
Reply: (edit)

sed 's#/[a-z]*.txt##' your_file

-jim


Report Offensive Follow Up For Removal

Response Number 3
Name: ahteck
Date: June 17, 2003 at 00:46:12 Pacific
Subject: Extract text using sed
Reply: (edit)

Yes! I gotit! But how can it write it into a file in the while loop?

Thanks Nails


Report Offensive Follow Up For Removal

Response Number 4
Name: nails
Date: June 17, 2003 at 08:58:16 Pacific
Subject: Extract text using sed
Reply: (edit)

Hi:

Here's three ways:

1) Redirect all of standard output to a file. If the script is called a.ss:

a.ss > output.file

2) Within the script, use file handles 1 and 2 to send all the output to a file:

exec 1>output.file 2>&1

3) Withing the while loop, send the output to a file:

while read str
do
echo ${str%/*}
done xfile > output.file
# need a less than sign between done
#and xfile. This forum wipes it out

Regards,

Nails


Report Offensive Follow Up For Removal

Response Number 5
Name: Peter
Date: July 9, 2003 at 07:46:44 Pacific
Subject: Extract text using sed
Reply: (edit)

Try this sed command. It should do the trick

sed 's:/[a-z]*.txt:/:g' infile > outfile


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Extract text using sed

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software