how to cut based on a string


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how to cut based on a string
# 1  
Old 09-05-2012
how to cut based on a string

Hi I have some data where each line will look something like this:

Code:
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 with:

Code:
RB: 0.9949; RB: 0.9951; RA: 0.995; RA: 0.996

theres about 50 odd lines like this so I want to loop over them all.

Currently I'm trying

Code:
awk 'BEGIN { FS = "R[A|B]:" } { for (i=2; i <=NF; i++) printf "%s\n", $i }'

but its not working.

If possible I'd also like to include the time at the beginning of each line, which is in the format hh.mm.ss.000

I am a beginner to UNIX and do not have that much knowledge, any help would be greatly appreciated!

Thanks in advance Smilie


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by gvc; 09-05-2012 at 12:38 PM.. Reason: code tags
# 2  
Old 09-05-2012
Not sure how the other other lines look like, but this could be generic enough:
Code:
awk -F"[;,]" '{for(i=1; i<=NF; i++){if($i ~ /(RA|RB):/){x?x=x";"$i:x=$i}}} {print x; x=a}' infile

This User Gave Thanks to zaxxon For This Post:
# 3  
Old 09-05-2012
perfect thank you so much!!, I added a ^ before the RA as I just realised some of the lines had RRA or RRB so it was returning those too. I've been trying to solve this by googling for the past few hours!

---------- Post updated at 11:38 AM ---------- Previous update was at 11:30 AM ----------

sorry - one more thing,

how would I get the timestamp at the beginning of each line included?

hh:mm:ss.000
# 4  
Old 09-06-2012
Code:
# awk -F"[;,]" -v date="$(date +%H:%M:%S).000;" '{for(i=1; i<=NF; i++){if($i ~ /(RA|RB):/){x?x=x";"$i:x=$i}}} {print date, x; x=a}' infile
105337.000;  RB: 0.9949; RB: 0.9951; RRA: 0.3; RA: 0.995; RA: 0.996

If the timestamp is instead supplied from the input file, you will have to show a relevant snippet of it.

Last edited by zaxxon; 09-06-2012 at 06:12 AM.. Reason: added the missing colons in the timestamp
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Cut each line based on the character size

Hello All, I have a file which contain below lines. The starting word of each line is call and the end line is semi colon. I need to find the character size of each line and then move it to a file. If the character size is more than 255 then I need to push that line to a next file and I need... (6 Replies)
Discussion started by: JoshvaPeter
6 Replies

2. UNIX for Dummies Questions & Answers

Display/Cut the characters based on match

I have input file like this update tablename set column1='ABC',column2='BBC' where columnx=1 and columny=100 and columnz='10000001' update tablename set column1='ABC',column2='BBC',column3='CBC' where columnx=1 and columny=100 and columnz='10000002' update tablename set column1='ABC' where... (1 Reply)
Discussion started by: nsuresh316
1 Replies

3. Shell Programming and Scripting

To cut a string based on last index of identifier

here below is sample string null pointer dereference of 'resourceList' where null is returned from a method/opt/bld/fetch/ds/interzone/notification/LocalLineStatusNotificationListener.java:79 null pointer dereference of 'reList' where null is returned from a... (3 Replies)
Discussion started by: vivek d r
3 Replies

4. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

5. UNIX for Dummies Questions & Answers

Cut from tables based on column values

Hello, I have a tab-delimited table that may contain 11,12 or 13 columns. Depending on the number of columns, I want to cut and get a sub table as shown below. However, the awk commands in the code seem to be an issue. What should I be doing differently? #cut columns 1-2,4-5,11 when 12 &... (3 Replies)
Discussion started by: Gussifinknottle
3 Replies

6. Emergency UNIX and Linux Support

awk cut column based on string

Using awk I required to cut out column contain word "-Tag" regardles of any order of contents and case INsensitive -Tag:messages -P:/var/log/messages -P:/var/log/maillog -K:Error -K:Warning -K:critical Please Guide ...... --Shirish Shukla ---------- Post updated at 05:58 AM... (15 Replies)
Discussion started by: Shirishlnx
15 Replies

7. UNIX for Advanced & Expert Users

cut and append the file based on id

Hi, I have a file which have set of rows and has to create separate files based on the id. Eg: 001_AHaris020 001_ATony030 002_AChris090 002_ASmit060 003_AJhon001 Output: I want three files like 001_A.txt, 002_A.txt and 003_A.txt. 001_A.txt should have ... (1 Reply)
Discussion started by: techmoris
1 Replies

8. 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

9. Shell Programming and Scripting

Cut based on Two Delimiters at one go

Hi I wanted to cut the feilds comming after % and After $ at one go can we do some thing like this cut -f 2 -d "%|$" (But it doesnot work) Input File BWPG %TCPRP1 $SCSPR000 BWPH %TCPRP1 $SCSPR003 BWPI %TRTYUP ResourceDescription="IMPRIMANTE " $BWOPTY BWPJ %ZOMBIE ... (4 Replies)
Discussion started by: pbsrinivas
4 Replies

10. UNIX for Dummies Questions & Answers

Cut a Variable into sub variables based on a delimiter

Hello All, I am novice on Shell Scripting. Any help on this is highly appreciated. I have a variable $VARIABLE="$some1|$some2|$some3" I need sub variables $SUBVAR1,$SUBVAR2,$SUBVAR3 which must be equal to $some1 , $some2 and $some3 respectively. It works fine with $SUBVAR1 =... (6 Replies)
Discussion started by: jingi1234
6 Replies
Login or Register to Ask a Question