Cut line up to a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cut line up to a string
# 8  
Old 05-31-2012
Quote:
Originally Posted by elixir_sinari
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}'

Great!Thanks a lot everybody!

---------- Post updated at 08:43 AM ---------- Previous update was at 06:26 AM ----------

I was wondering what if the required field is not inside "" but inside parentheses or brackets?
Then is it something like this?

Code:
echo things | awk -F\(\) '{for(i=1;i<=NF;i++) if($i ~ /^id:/) print $i}'

Because this is not workingSmilie
# 9  
Old 05-31-2012
Use square brackets to use more than one kind of characters as field separator and do not forget to use quotes around it to protect it from the shell.

-----
# 10  
Old 05-31-2012
Quote:
Originally Posted by Scrutinizer
Use square brackets to use more than one kind of characters as field separator and do not forget to use quotes around it to protect it from the shell.

-----
you mean something like this:

Code:
awk -F["\(\)"]

because it's not working and I assume there's syntax error

---------- Post updated at 11:45 AM ---------- Previous update was at 09:40 AM ----------

Quote:
Originally Posted by elixir_sinari
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}'

So I tried the following in another case

Code:
if [[ "$line" == *\<img* ]] 
then
  echo found!
  echo $line | awk '{for(i=1;i<=NF;i++) if($i ~ /^img/) print $i}' #| wc -l
fi

now in my file the lines look like this:

Code:
kkk fff hhh<img dddddd <img
kkk fff hhh aaaaaa <img

the command

Code:
echo found!

is executed for both lines ok
but the next echo prints 1 for both lines instead of printing 2 for the
first line and 1 for the second
I assumed the problem is that it doesn't count the <img if before there're letters such as

Code:
dddd<img

although the if statement sees it.So it must be a problem with the awk in that case
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