Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

extracting first few chars from file 3

Status
Not open for further replies.

YYYYUU

Programmer
Dec 13, 2002
47
GB
I was using "head -1" to extract the first line from a file and put this in variable ${FIRSTLINE}.

How do I get the first 10 characters from ${FIRSTLINE}?

Many thanks.
 
No need to use head. awk will get the first line and the first 10 characters into a variable:

VARIABLE=`awk 'NR == 1{print substr($0,1,10)}' your_file`


-jim
 
In Korn Shell you can use...
[tt]
typeset -L10 FIRST_TEN

FIRST_TEN=$(head -1 /dir/filename)
[/tt]
The [tt]typeset[/tt] command changes the way the variable works. The "L" left justifies whatever you assign to it. The "10" forces the length to 10, no matter what you assign to it.

Hope this helps.
 
How to extract last word from a string?
e.g.: in the string "/home/myid"
expected result: lastword=myid

Thanks

Mike
 
#!/bin/ksh

a='/home/dir/myid'

last="${a##*/}"

echo "a->[${a}] last->[${last}]"
vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks vlad,
It works fine. How about to extract the first or second.... word from a string &quot;abc defg mn xyz&quot;? Thanks again.

Mike
 
#!/bin/ksh
str=&quot;abc defg mn xyz&quot;

first=$(echo ${str} | nawk '{print $1}')
second=$(echo ${str} | nawk '{print $2}') vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top