- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- select the nth word on a line in an ascii file
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2001 02:33 AM
11-14-2001 02:33 AM
What's the easiest way for assigning the wth word of a line l in a textfile t to variable v in a script ?
Franky
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2001 02:42 AM
11-14-2001 02:42 AM
Re: select the nth word on a line in an ascii file
you can use sed or vi:
cat file | sed "/string/$VARIABLE/g"
or use the vi command
g/string/s//$VARIABLE/g
hope this will help
Gideon
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2001 02:44 AM
11-14-2001 02:44 AM
SolutionIf you have any word to identified your line, try
V=$(cat FILE | grep WORD | awk '{ print $n }')
where FILE is the textfile's name, and n the position of the word you need.
R
Fr??d??ric
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2001 02:46 AM
11-14-2001 02:46 AM
Re: select the nth word on a line in an ascii file
As a one-liner:
v=`awk 'NR==l{print $w}' w=$w l=$l $t`
or in a script:
================================
#!/bin/ksh
w=$1
l=$2
t=$3
v=`awk 'NR==l{print $w}' w=$w l=$l $t`
echo $v
================================
so if /tmp/file contains:
a b c
d e f
g h ij k
345 234 234
# ./script.sh 3 2 /tmp/file
h
Rgds, Robin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2001 02:47 AM
11-14-2001 02:47 AM
Re: select the nth word on a line in an ascii file
v=`echo $LINE | cut -f 67`
assuming you transfered the text-line in charge to the variable LINE and the word you want is word 67 and you consider a "word" as whitespace-delimited as usual in C.
Hope this helps
Volker
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2001 02:54 AM
11-14-2001 02:54 AM
Re: select the nth word on a line in an ascii file
best way is to grep line and select required word with awk '{ print $n }'.
Later,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2001 03:14 AM
11-14-2001 03:14 AM
Re: select the nth word on a line in an ascii file
if you want to process an entire file, here is another aproach, which should no even be limited to a max of 199 words per line as an awk solution:
#!/usr/bin/sh
# Set Word to be processed -1
WTH=3
assign()
{
if [ $# -ge $WTH ]
then
shift $WTH
WORD=$1
else
WORD=''
fi
}
while read LINE
do
assign $LINE
# now $WORD contains the word in charde
v=$WORD
# Do whatever you want with $v
echo processing: $v
done
Works like:
# sh procline.sh
a b c d e f
processing: d
one two three four five
processing: four
a b c d e f g h
processing: d
a
processing:
#
I like these puzzles
Volker
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2001 04:39 AM
11-14-2001 04:39 AM
Re: select the nth word on a line in an ascii file
The solutions that suits me best is the one with the grep - awk combination.
Regards,
Franky