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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String Manipulation

Status
Not open for further replies.

MIKENY1

IS-IT--Management
Feb 4, 2003
2
US
To All:

I have the following string Joe Smith. I would like AWK to create two separate strings,
Joe
Smith

Can I used the space as a separator ? For example my test string could also be Joseph Smithers. Is there an Awk command to separate the string when a blank space is encountered ?

Many Thanks

 

By default, awk breaks up the input line into fields with the default delimiter being one or more spaces. So this awk script should do what you want:

{print $1;print $2} CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
Try split().
awk ' BEGIN {
split("Joe Smith",arr," ")
printf "%s\n%s\n", arr[1], arr[2]
}'
 
echo "Joe Smith" | nawk -v OFS='\n' '{$1=$1; print}'

OR

#----------------- split.awk
# nawk -f split.awk myFile.txt
BEGIN {
OFS="\n"
}
{ $1=$1;print}


OR

echo "Joe Smith" | tr ' ' '\n' vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
[tt]$ echo 'Joe Smith'|read FIRSTNAME SECONDNAME
$ echo $FIRSTNAME
Joe
$ echo $SECONDNAME
Smith
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top