|
google site
|
|||||||
| Forums | Register | Blog | Man Pages | Forum Rules | Links | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
#8
|
||||
|
||||
|
using cut command we can achive this: see the example: Code:
$cat line abcd | fghfh | qwer | ertete $cut -d"|" -f 1-3 line abcd | fghfh | qwer $cut -d"|" -f 4 line ertete -d used for specifying the delimiter. -f used for specify the field numbers. Last edited by ungalnanban; 02-23-2010 at 01:55 AM.. Reason: spell mistake |
| Sponsored Links | ||
|
|
|
#9
|
||||
|
||||
|
consider that the data file is having the content as Code:
abcd | fghfh | qwer | ertete So, Code:
cat data | cut -d"|" -f -3 this will give Code:
abcd | fghfh | qwer And, Code:
cat data | cut -d"|" -f 4 this will give ertete you can also use the sed script as follows Code:
cat data | sed -e "s/\(.*|\)/\1\n/" The output you will get as follows Code:
abcd | fghfh | qwer | ertete Last edited by scottn; 02-27-2010 at 07:53 PM.. Reason: Code tags please... |
|
#10
|
||||
|
||||
|
Dear Friend, You can use the following bash script. Code:
var="abcd | fghfh | qwer | ertete"; var1=`echo $var| cut -c 1-20`; echo 'line1='$var1; var2=`echo $var|cut -c 22-28`; echo 'line2='$var2; The output is line1=abcd | fghfh | qwer line2= ertete |
|
#11
|
||||
|
||||
|
We can also achieve it in the following way.This script will work,if you don't know the number of fields also. Code:
str="abcd | fghfh | qwer | ertete" echo $str abcd | fghfh | qwer | ertete $line1=echo $str | rev | cut -d '|' -f 2- | rev $line2=echo $str | rev | cut -d '|' -f 1 | rev echo $line1 abcd | fghfh | qwer echo $line2 ertete |
|
#12
|
|||
|
|||
|
Dear Amit, You can also use the below script..... Code:
cat data abcd | fghfh | qwer | ertete aaaa | bbbbb | cccc | dddddd Code:
while read input
do
line1=`echo $input|awk -F\| '{print $1" |" $2" |" $3}'`
line2=`echo $input|awk -F\| '{print $4}'`
echo "line1=$line1"
echo "line2=$line2"
done < dataOut put is : Code:
line1=abcd | fghfh | qwer line2= ertete line1=aaaa | bbbbb | cccc line2= dddddd Last edited by scottn; 02-27-2010 at 03:03 PM.. Reason: Please use code tags |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Trouble cutting characters into a string. | Makaer | Shell Programming and Scripting | 0 | 07-31-2009 07:07 AM |
| Cutting segment of a string | msb65 | Shell Programming and Scripting | 5 | 11-04-2008 10:00 PM |
| CUT command - cutting characters from end of string | JWilliams | AIX | 2 | 01-28-2008 08:12 AM |
| cutting part of string | dhaval_khamar | Shell Programming and Scripting | 3 | 07-25-2005 10:18 AM |
| Cutting Up a String | lesstjm | Shell Programming and Scripting | 4 | 09-21-2004 11:40 AM |