Include information in certain columns using grep and awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Include information in certain columns using grep and awk
# 1  
Old 09-03-2014
Include information in certain columns using grep and awk

HI all,
I have data in a file that looks like this:
Code:
1       HOW     _       NNP     NNP     _       3       nn      _       _
2       DRUGS   _       NNP     NNP     _       3       nn      _       _
3       ACT     _       NNP     NNP     _       0       null    _       _
4       :       _       :       :       _       3       punct   _       _
5       GENERAL _       NNP     NNP     _       7       nn      _       _
6       PRINCIPLES      _       NNPS    NNPS    _       7       nn      _      _
7       Pharmacology    _       NNP     NNP     _       10      nsubjpass      __
8       can     _       MD      MD      _       10      aux     _       _
9       be      _       VB      VB      _       10      auxpass _       _
10      defined _       VBN     VBN     _       3       parataxis       _      _

I need to use 'awk' to include some information in the second column of those lines which have 'N*' in the 4th column.
Is there an easy 'grep' that will allow me to do this?

The desired result is the following:
Code:
1       HOW-x-     _       NNP     NNP     _       3       nn      _       _
2       DRUGS-x-   _       NNP     NNP     _       3       nn      _       _
3       ACT-x-     _       NNP     NNP     _       0       null    _       _
4       :       _       :       :       _       3       punct   _       _
5       GENERAL-x- _       NNP     NNP     _       7       nn      _       _
6       PRINCIPLES-x-      _       NNPS    NNPS    _       7       nn      _      _
7       Pharmacology-x-    _       NNP     NNP     _       10      nsubjpass      __
8       can     _       MD      MD      _       10      aux     _       _
9       be      _       VB      VB      _       10      auxpass _       _
10      defined _       VBN     VBN     _       3       parataxis       _     _

Thank you in advance for assitance.

Last edited by owwow14; 09-03-2014 at 10:42 AM..
# 2  
Old 09-03-2014
Hello owwow,

Following may help.

Code:
awk '($4 ~ /^N.*/){$2=$2"-x"} 1' OFS="\t"  filename

Output will be as follows.

Code:
1       HOW-x   _       NNP     NNP     _       3       nn      _       _
2       DRUGS-x _       NNP     NNP     _       3       nn      _       _
3       ACT-x   _       NNP     NNP     _       0       null    _       _
4       :       _       :       :       _       3       punct   _       _
5       GENERAL-x       _       NNP     NNP     _       7       nn      _       _
6       PRINCIPLES-x    _       NNPS    NNPS    _       7       nn      _       _
7       Pharmacology-x  _       NNP     NNP     _       10      nsubjpass       __
8       can     _       MD      MD      _       10      aux     _       _
9       be      _       VB      VB      _       10      auxpass _       _
10      defined _       VBN     VBN     _       3       parataxis       _      _

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 09-03-2014
@owwow14 there is no N in the third column. There is in the 4th and 5th column but this is inconsistent with your sample output. Please clarify..
# 4  
Old 09-03-2014
Thanks for pointing out the error (it should have been 4th and not 3rd). I just updated the original post.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed / awk / grep to extract information from log

Hi all, I have a query that runs that outputs data in the following format - 01/09/12 11:43:40,ADMIN,4,77,Application Group Load: Name(TESTED) LoadId(5137-1-0-1XX-15343-15343) File(/dir/dir/File.T03.CI2.RYR.2012009.11433350806.ARD) InputSize(5344) OutputSize(1359) Rows(2) Time(1.9960)... (8 Replies)
Discussion started by: jeffs42885
8 Replies

2. Shell Programming and Scripting

Include information and change column order while maintaing white spaces

I need to change the order of the columns of data that looks like this: The original data is in 6 tab-separated columns. FACTSHEET factsheet NN 1 5 DEP WHAT what WP 2 3 SBJ IS be VBZ 3 1 NMOD AIDS AIDS NP ... (1 Reply)
Discussion started by: owwow14
1 Replies

3. Shell Programming and Scripting

Use less pipe for grep or awk sed to print the line not include xx yy zz

cat file |grep -v "xx" | grep -v "yy" |grep -v "zz" (3 Replies)
Discussion started by: yanglei_fage
3 Replies

4. Shell Programming and Scripting

Need to include two more columns in the file using awk

Hi, I have a input file with many records as below: 1J4RR4GG0BC508200 68646 1 N M i want my output file to be like with columns included dgismdh and timestamp : Example: 1J4RR4GG0BC508200 68646 1 N M dgismdh 2012-02-21 07:22:25.98591 How to do it.can we do using awk? Pls help. (6 Replies)
Discussion started by: sonam273
6 Replies

5. Shell Programming and Scripting

copying file information using awk & grep

Hi, TASK 1: I have been using this code to print the information of files kept at "/castor/cern.ch/user/s/sudha/forPooja" in some text file name FILE.txt. rfdir /castor/cern.ch/user/s/sudha/forPooja | grep data | awk '{print "rfio:///castor/cern.ch/user/s/sudha/forPooja/"$9}' > FILE.txt ... (6 Replies)
Discussion started by: nrjrasaxena
6 Replies

6. Shell Programming and Scripting

grep/awk to only print lines with two columns in a file

Hey, Need some help for command to print only lines with two columns in a file abc 111 cde 222 fgh ijk 2 klm 12 23 nop want the ouput to be abc 111 cde 222 ijk 2 Thanks a lot in advance!!! (3 Replies)
Discussion started by: leo.maveriick
3 Replies

7. Shell Programming and Scripting

awk (gawk) grep & columns

Hi, I'm working with gawk (on DOS) today. A goal is: find a string for-instance '123', cut a line in two columns and write second one. The problem is: command line works OK, awk file doesn't. But I would like to work with file because there are many strings to find. input: line command: awk... (4 Replies)
Discussion started by: frajer
4 Replies

8. Shell Programming and Scripting

Display output as columns using grep/awk/sed

I have several files with say something like cat sparrow I can grep for "cat" and "sparrow" and usually the output is one below the other cat sparrow How can I view these as columns say Pets Birds cat sparrow Would be great if this can be on command line using awk or... (1 Reply)
Discussion started by: gubbu
1 Replies

9. UNIX for Dummies Questions & Answers

why does tar sometimes include directory information?

hi, say I have dirA/file1 dirB/file2 and I tar them up, and then do zcat Tar.tar | tar tvf - Sometimes I will see: dirA/ dirA/file1 dirB/ dirB/file2 yet other times I will see (4 Replies)
Discussion started by: JamesByars
4 Replies

10. Shell Programming and Scripting

awk / grep / Prints all columns except the first one

I have the a file with the following data. File Content. 1249 snf06.3.0 sw version 1.1 code MD5 192F MD4 MD3 1248 sns06.3.0 sw version 1.1 code MD5 192F MD12 1250 sns06.3.0 sw version 1.1 code MD5 192F0\ MD8 1241 sns06.3.0 sw code MD5 19 1243 sn06.3.0 sw version 1.1 code MD5 19 12... (17 Replies)
Discussion started by: knijjar
17 Replies
Login or Register to Ask a Question