Cut line up to a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cut line up to a string
# 1  
Old 05-31-2012
Cut line up to a string

hi all

In my bash script I want to cut a line up to a specific string and keep the rest of it but only up to a ".How can I do that?I imagine something with sed..
Let's say my line is:

Code:
Jennifer Jones (student) "id:376765748587/7465674775" NewYork

and i only want to keep:

Code:
id:376765748587/7465674775

(some lines have more or less words so the number of words is not fixed for a line)

Last edited by Scrutinizer; 05-31-2012 at 07:42 AM.. Reason: code tags
# 2  
Old 05-31-2012
Code:
 echo "Jennifer Jones (student) \"id:376765748587/7465674775\" NewYork"|cut -d"\"" -f2

This User Gave Thanks to donadarsh For This Post:
# 3  
Old 05-31-2012
Code:
 
echo 'Jennifer Jones (student) "id:376765748587/7465674775" NewYork' | awk -F\" '{print $2}'

This User Gave Thanks to itkamaraj For This Post:
# 4  
Old 05-31-2012
Code:
echo 'Jennifer Jones (student) "id:376765748587/7465674775" NewYork'|sed 's/.*"\([^"]*\)".*/\1/g'

This User Gave Thanks to elixir_sinari For This Post:
# 5  
Old 05-31-2012
Code:
while IFS=\" read user id town
do
  echo "$id"
done < file

This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 05-31-2012
Quote:
Originally Posted by elixir_sinari
Code:
echo 'Jennifer Jones (student) "id:376765748587/7465674775" NewYork'|sed 's/.*"\([^"]*\)".*/\1/g'

in some lines there're fields with more than one phrace inside "".For example:

Code:
Brad Robins "worker in FGD " LA "id:3476543774/638957685"

so i want to keep only the ones that start with id and finish with a ".
Also in some lines there's more than one field for id.For example:

Code:
Brad Robins "worker in FGD" LA "id:3476543774/638957685" alternative::"id:7567756/38584896"

So is there a way in every line to keep every field that starts with the string 'id' and ends with a "?

The fields for every line might be in different order or column so it's not fixed that in the first column is the name for example,or the id in the 3rd..
# 7  
Old 05-31-2012
Code:
echo 'Brad Robins "worker in FGD" LA "id:3476543774/638957685" alternative::"id:7567756/38584896"'|awk -F\" '{for(i=1;i<=NF;i++) if($i ~ /^id:/) print $i}'

This User Gave Thanks to elixir_sinari For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

How to add a new string at the end of line by searching a string on the same line?

Hi, I have a file which is an extract of jil codes of all autosys jobs in our server. Sample jil code: ************************** permission:gx,wx date_conditions:yes days_of_week:all start_times:"05:00" condition: notrunning(appDev#box#ProductLoad)... (1 Reply)
Discussion started by: raghavendra
1 Replies

2. Shell Programming and Scripting

Cut the string

---------- Post updated at 10:31 AM ---------- Previous update was at 10:28 AM ---------- Hello, I am trying to get the string cut based on the following needs: String1=Submitted request 25574824 for CONCURRENT SQLAP RUAPACTUALSEXT Y RUAPACTUALS122313100616.dat "2013/01/12 14:50:44"... (6 Replies)
Discussion started by: cartrider
6 Replies

3. Shell Programming and Scripting

Cut the string

Hi in a directory i've files having the following name for_category_info_19990101984301 for_catgry_meta_19991111214601 ------- I just want the name till year and month i.e; for_category_info_199901 for_catgry_meta_199911 How can i achieve the above string Thanks (2 Replies)
Discussion started by: smile689
2 Replies

4. UNIX for Dummies Questions & Answers

how to cut based on a string

Hi I have some data where each line will look something like this: Time, name, i.d number, RB: 0.9949; RMQA: 0.0005; RB: 0.9951; RRA: 0.3; RA: 0.995; RA: 0.996; EA: 0.99105 etc. I want to cut out all the RB: and RA:'s with the numbers after. so in the above example i'd be left... (3 Replies)
Discussion started by: gvc
3 Replies

5. Shell Programming and Scripting

backward string cut

I need only the last .ko files to be stripped from the whole result., ie libiscsi2.ko, scsi_transport_iscsi2.ko etc.. kernel/drivers/scsi/libiscsi2.ko kernel/drivers/scsi/scsi_transport_iscsi2.ko kernel/drivers/scsi/scsi_transport_iscsi.ko kernel/fs/nls/nls_utf8.ko... (4 Replies)
Discussion started by: anilcliff
4 Replies

6. Slackware

How should I cut this line using cut and grep?

not sure how to do it. wan't to delete it using cut and grep ince i would use it in the shell. but how must the command be? grep "64.233.181.103 wwwGoogle.com" /etc/hosts | cut -d the delimeter is just a space. can you help meplease. :D (1 Reply)
Discussion started by: garfish
1 Replies

7. Shell Programming and Scripting

Cut string from a line into a variable

Hi, I am working on a ksh script and I´m stuck on the following: I have to get the pthread_id from a procstack file for a particular tid#. ---------- tid# 1274057 (pthread ID: 1800) ---------- ---------- tid# 1736913 (pthread ID: 4019) ---------- ---------- tid# 1478705 (pthread ID: ... (7 Replies)
Discussion started by: tmf33uk
7 Replies

8. Shell Programming and Scripting

cut a string in a textfile line per line

i need to cut the string in a textfile but each line has a specific way of cutting it (different lengths) i have a for loop that gets the string line per line, then each line has to be compared: for x in `cat tmp2.txt`; do if; then echo 'BAC' elif ... (6 Replies)
Discussion started by: izuma
6 Replies

9. Shell Programming and Scripting

To cut end string from line

HI, I want to cut end string from line. e.g. i have following input line /users/home/test.txt I want to get end string 'test.txt' from above line and length of that end string will change and it always start after '/'. Thanks, Visu (7 Replies)
Discussion started by: visu
7 Replies

10. Shell Programming and Scripting

read string, check string length and cut

Hello All, Plz help me with: I have a csv file with data separated by ',' and optionally enclosed by "". I want to check each of these values to see if they exceed the specified string length, and if they do I want to cut just that value to the max length allowed and keep the csv format as it... (9 Replies)
Discussion started by: ozzy80
9 Replies
Login or Register to Ask a Question