How to return only specific words in a line?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to return only specific words in a line?
# 1  
Old 02-26-2013
How to return only specific words in a line?

Hi,

I'm a bash newbie and have a data set like this
Code:
idxxx1   something   something   marker_id_132=rsxxx;marker_id_135=rsxxx   
idxxx2   something   something   marker_id_132=rsxxx;marker_id_135=rsxxx   
idxxx3   something   something   marker_id_132=rsxxx;marker_id_135=rsxxx   
...

and I want to find specific idxxx numbers and return (only)
Code:
idxxx1   marker_id_132=rsxxx
idxxx3   marker_id_132=rsxxx

I've used
Code:
egrep "idxx1|idxx3" file.txt

but I would rather have only idxx and marker_id_132=rsxxx than the entire line


I would appreciate any help!
Hanna

Last edited by vbe; 02-26-2013 at 10:00 AM.. Reason: code tags...
# 2  
Old 02-26-2013
Bash regexp:
Code:
#!/bin/bash

while read line
do
        l="${line##* }"

        [[ "$line" =~ ^idxxx1 ]] && printf "%s %s\n" "idxxx1" "${l%;*}"
        [[ "$line" =~ ^idxxx3 ]] && printf "%s %s\n" "idxxx3" "${l%;*}"

done < filename

# 3  
Old 02-26-2013
Code:
#!/bin/bash
while read key other
do
 l=${other##* }
 case $key in
 idxxx1|idxxx3) printf "%s %s\n" "$key" "${l%%;*}"
 ;;
 esac
done < data_set

Or let awk do it
Code:
#!/bin/sh
awk '$1~/^(idxxx1|idxxx3)$/ { split($NF,a,";"); printf "%s %s\n", $1, a[1] }
' data_set

# 4  
Old 02-26-2013
Just using your example,
egrep "idxxx1|idxxx3" file | awk '{print $1 "\t" $4}' | awk -F';' '{print $1}'

Step1:
Code:
$ egrep "idxxx1|idxxx3" file
$ idxxx1   something   something   marker_id_132=rsxxx;marker_id_135=rsxxx
   idxxx3   something   something   marker_id_132=rsxxx;marker_id_135=rsxxx

Step2:
Code:
$ egrep "idxxx1|idxxx3" file | awk '{print $1 "\t" $4}' 
$ idxxx1  marker_id_132=rsxxx;marker_id_135=rsxxx
   idxxx3  marker_id_132=rsxxx;marker_id_135=rsxxx

Step3:
Code:
$ egrep "idxxx1|idxxx3" file | awk '{print $4}' | awk -F';' '{print $1}'
$ idxxx1  marker_id_132=rsxxx
   idxxx3  marker_id_132=rsxxx

Infact you can use a Variable for your search
Code:
str="idxxx1|idxxx3"; egrep $str file | awk '{print $1 "\t" $4}' | awk -F';' '{print $1}'

Hope it helps.
# 5  
Old 02-26-2013
How about:-
Code:
egrep "idxxx1|idxxx3" file | tr ";tab" "  "|tr -s " "|cut -f1,4 -d" "

So:-
  • The egrep part selects the records
  • The first tr changes the semi-colon or a tab into a space. Note There are two spaces in the second string. (I can't tell if your output is tab or space separated)
  • The second tr changes multiple spaces to a single space
  • The cut selects the columns you are after.


Does this work for you? It's sometimes easier to read that learning awk, but there is a performance cost for larger files.


I hope that this helps,
Robin
Liverpool/Blackburn
UK
# 6  
Old 02-26-2013
Quote:
Originally Posted by angad.makkar
egrep "idxxx1|idxxx3" file | awk '{print $1 "\t" $4}' | awk -F';' '{print $1}'
Please note that there is no need to use egrep or grep with awk because awk is capable of doing what grep or egrep can do thus you can save a pipeline.

Here is another approach:
Code:
awk '/^idxxx1/||/^idxxx3/{$0=$1" "$NF;sub(/;.*/,x); print}' file

These 2 Users Gave Thanks to Yoda For This Post:
# 7  
Old 05-11-2013
I have a situation like, I have to read only the timestamp value from a line.
ex: Date and Time : 2013-05-11 12:12:34 MST
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep only words containing specific string

Hello, I have two files. All urls are space seperated. source http://xx.yy.zz http://df.ss.sd.xz http://09.09.090.01 http://11.22.33 http://canada.xx.yy http://01.02.03.04 http://33.44.55 http://98.87.76.65 http://russia.xx.zz http://aa.tt.xx.zz http://1w.2e.3r.4t http://china.rr.tt ... (4 Replies)
Discussion started by: baris35
4 Replies

2. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

3. Shell Programming and Scripting

Search a column a return a set of words

Hi I have two files. One is a text file consisting of sentences i.e. INPUT.txt and the second file is SEARCH.txt consisting of two or three columns. I need help to write a script to search the second column of SEARCH.txt for each set of five words (blue color as set one and green color as set... (6 Replies)
Discussion started by: my_Perl
6 Replies

4. Shell Programming and Scripting

Replace specific words with nothing

Hi I have a file like that contains infomation about genes exons introns made as a single string. i am just planning to get the gene name alone with out any extra information. intergenic_Nedd4_exon_0_F Gapvd1_intron_24_R Gapvd1_exon_25_Rmy output file should be intergenic_Nedd4 Gapvd1... (13 Replies)
Discussion started by: raj_k
13 Replies

5. Shell Programming and Scripting

how to print specific lines or words

Hi, Please have a look on below records. STG_HCM_STATE_DIS_TAX_TBL.1207.Xfm: The value of the row is: EMPLID = 220677 COMPANY = 919 BALANCE_ID = 0 BALANCE_YEAR = 2012 STG_HCM_STATE_DIS_TAX_TBL.1207.Xfm: ORA-00001: unique constraint (SYSADM.PS_TAX_BALANCE) violated ... (4 Replies)
Discussion started by: Sachin Lakka
4 Replies

6. Shell Programming and Scripting

Using awk to read a specific line and a specific field on that line.

Say the input was as follows: Brat 20 x 1000 32rf Pour 15 p 1621 05pr Dart 10 z 1111 22xx My program prompts for an input, what I want is to use the input to locate a specific field. Like if I type in, "Pou" then it would return "Pour" and just "Pour" I currently have this line but it is... (6 Replies)
Discussion started by: Bungkai
6 Replies

7. UNIX and Linux Applications

Reading a file for specific words

Hi I have a script where the user calls it with arguments like so: ./import.sh -s DNSNAME -d DBNAME I want to check that the database entered is valid by going through a passwd.ds file and checking if the database exists there. If it doesn't, the I need to send a message to my log... (4 Replies)
Discussion started by: ladyAnne
4 Replies

8. Shell Programming and Scripting

Copying x words from end of line to specific location in same line

Hello all i know it is pretty hard one but you will manage it all after noticing and calculating i find a rhythm for the file i want to edit to copy the last 12 characters in line but the problem is to add after first 25 characters in same line in other way too copy the last 12 characters... (10 Replies)
Discussion started by: princesasa
10 Replies

9. Shell Programming and Scripting

To fetch specific words from a file

Hi All, I have a file like this,(This is a sql output file) cat query_file 200000029 12345 10001 0.2 0 I want to fetch the values 200000029,10001,0.2 .I tried using the below code but i could get... (2 Replies)
Discussion started by: girish.raos
2 Replies

10. UNIX for Dummies Questions & Answers

return a word between two words

how do i get a word that exists between two words eg: this is bryan My input to command would be this and bryan and output should be 'is' Is there a command i can use? (4 Replies)
Discussion started by: bryan
4 Replies
Login or Register to Ask a Question