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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Extract text using sed

Reply to Message Icon

Name: ahteck
Date: June 16, 2003 at 21:38:57 Pacific
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.




Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: June 16, 2003 at 23:22:27 Pacific
Reply:

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


0

Response Number 2
Name: Jimbo
Date: June 17, 2003 at 00:34:51 Pacific
Reply:

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

-jim


0

Response Number 3
Name: ahteck
Date: June 17, 2003 at 00:46:12 Pacific
Reply:

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

Thanks Nails


0

Response Number 4
Name: nails
Date: June 17, 2003 at 08:58:16 Pacific
Reply:

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


0

Response Number 5
Name: Peter
Date: July 9, 2003 at 07:46:44 Pacific
Reply:

Try this sed command. It should do the trick

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


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Extract text using sed

Extracting data from file using sed www.computing.net/answers/unix/extracting-data-from-file-using-sed/7660.html

How to use 'sed' on windows XP. www.computing.net/answers/unix/how-to-use-sed-on-windows-xp/6605.html

Extracting text between placeholder www.computing.net/answers/unix/extracting-text-between-placeholder/8233.html