Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
google site



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 !!

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Display Modes
  #8  
Old 02-23-2010
ungalnanban's Avatar
Registered User
 

Join Date: Feb 2010
Location: Chennai
Posts: 112
Thanks: 1
Thanked 4 Times in 3 Posts
Thumbs up

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  
Old 02-23-2010
thillai_selvan's Avatar
Registered User
 

Join Date: Feb 2010
Location: Chennai
Posts: 182
Thanks: 0
Thanked 0 Times in 0 Posts
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  
Old 02-26-2010
murugaperumal's Avatar
Registered User
 

Join Date: Feb 2010
Location: Chennai
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
Smile

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  
Old 02-27-2010
vivekraj's Avatar
Registered User
 

Join Date: Feb 2010
Location: Chennai
Posts: 112
Thanks: 0
Thanked 0 Times in 0 Posts
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  
Old 02-27-2010
Registered User
 

Join Date: Feb 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
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 < data

Out 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
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 08:00 AM.