Help with cutting a string


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with cutting a string
# 8  
Old 02-23-2010
MySQL

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 02:55 AM.. Reason: spell mistake
# 9  
Old 02-23-2010
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 Scott; 02-27-2010 at 08:53 PM.. Reason: Code tags please...
# 10  
Old 02-26-2010
Bug

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
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
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 Scott; 02-27-2010 at 04:03 PM.. Reason: Please use code tags
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cutting string values from a file

My file looks like this.... User:SYSTEM,O/S User:oracle,Process:3408086,Machine:hostname ,Program:sqlplus@hostname (TNS V1-V,Logon Time:25-JUL-20 14 13:36 I want to get the date and time which is displayed after the 'Logon time'. (5 Replies)
Discussion started by: Nagesh_1985
5 Replies

2. Shell Programming and Scripting

Cutting a string using more than one character as delimiter

Hi , I have a set of files in a folder which i need to cut in to two parts.... Sample files touch AE_JUNFOR_2014_MTD_2013-05-30-03-30-02.TXT touch AE_JUNFOR_2014_YTD_2013-05-30-03-30-02.TXT touch temp_AE_JUNFOR_2014_MTD_2013-05-30-03-30-02.TXT touch... (4 Replies)
Discussion started by: chillblue
4 Replies

3. Shell Programming and Scripting

Cutting a string and storing it in a variable

Hello I have a script: path=test1 echo "${path%?}" till this the program is successful in deleting hte last character i.e. "1" and displays an output --> test. After this how can i save this output to another variable. (2 Replies)
Discussion started by: Kishore920
2 Replies

4. Shell Programming and Scripting

Cutting string inside if

I was trying the below statement if It is working fine if I run it in a test file. but not working, when I am trying in my actual script. Error: : "${FXML_line:1129:1}": bad substitution Thanks in advance :) PS: Above if block I have a while loop which is reading a... (4 Replies)
Discussion started by: ezee
4 Replies

5. Shell Programming and Scripting

Cutting the value from a string

Hi all I have a string variable ${WHERE_SQL1} where i want to cut the first value of a variable. Eg ${WHERE_SQL1} = 'Where a.id =.................' I the string to be 'a.id =.......' Thanks in advance (2 Replies)
Discussion started by: theeights
2 Replies

6. Shell Programming and Scripting

Trouble cutting characters into a string.

I just have a couple of quick questions. I am having trouble with this cut. I am basically trying to cut the string so that i can insert the users guess at the appropriate point in the string. $letters is the character count of the $word. What it seems to do is cut the character into the... (0 Replies)
Discussion started by: Makaer
0 Replies

7. UNIX for Dummies Questions & Answers

Cutting a string based on the third occcurence of a character

Hello, I am new to unix hence struggling with my requirement. I have a string like : ECR/CHQ/GBP/12345.out I need to get only the ECR/CHQ/GBP portion of the string(cut the string based on the third occurrence of / )...How do it do it? Many thanks (3 Replies)
Discussion started by: valluvan
3 Replies

8. Shell Programming and Scripting

Cutting segment of a string

Hi, I am using bash. My question concerns cutting out segments of a string. Given the following filename: S2002254132542.L1A_MLAC.x.hdf I have been able to successfully separate the string at the periods (.): $ L1A_FILE=S2002254132542.L1A_MLAC.x.hdf $ BASE=$(echo $L1A_FILE | awk -F.... (5 Replies)
Discussion started by: msb65
5 Replies

9. Shell Programming and Scripting

cutting part of string

Hi, I wanted to cut a specific portion from given string. How would I do that? Example: /u09/core/inbound/abc.txt is my string. I want abc.txt in a variable. Please help me. Regards, Dhaval (3 Replies)
Discussion started by: dhaval_khamar
3 Replies

10. Shell Programming and Scripting

Cutting Up a String

I have a file name coming in as such <string>_YYYYMMDD.DAT The string could be anything. I want to cut out the date and put it in a variable. Can someone help me with this? (4 Replies)
Discussion started by: lesstjm
4 Replies
Login or Register to Ask a Question