Grep and append


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep and append
# 1  
Old 01-22-2015
HP Grep and append

Have a string as below in a file.

"
Code:
anohter boy has id 000921 and girl has id=655 of roll number

"

using grep below commands with grep commands but able to get one string at a time, not able to append.

Code:
less <filename> | grep -o -P '(?<=id ).*(?=and )'

Code:
 
less <filename> | grep -o -P '(?<=id\= ).*(?=of )'

how to get output as :
Code:
000921 655

thanks

Last edited by Scrutinizer; 01-22-2015 at 04:01 AM.. Reason: code tags
# 2  
Old 01-22-2015
try

Code:
grep -o -P '(?<=id ).*?(?= and )|(?<=id=).*?(?= of )' filename | paste -d " " - -

Or

Code:
grep -Po '(?<=id( |=)).*?(?= (and|of) )' filename | paste -d " " - -


Last edited by Scrutinizer; 01-22-2015 at 11:06 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Inconsistent `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l`

i have this line of code that looks for the same file if it is currently running and returns the count. `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l` basically it is assigned to a variable ISRUNNING=`ps -eaf -o args | grep -i sfs_pcard_load_file.ksh |... (6 Replies)
Discussion started by: wtolentino
6 Replies

2. UNIX for Dummies Questions & Answers

Bash - CLI - grep - Passing result to grep through pipe

Hello. I want to get all modules which are loaded and which name are exactly 2 characters long and not more than 2 characters and begin with "nv" lsmod | (e)grep '^nv???????????? I want to get all modules which are loaded and which name begin with "nv" and are 2 to 7 characters long ... (1 Reply)
Discussion started by: jcdole
1 Replies

3. UNIX for Dummies Questions & Answers

Grep search and append

I'm sure glad I found this forum... This is my first post, so please be gentle... ;) I tried searching everywhere, but the terminology is so common that I cannot find a solution to my problem. I'm looking for a GREP statement to do the following... I need a search and append function... (5 Replies)
Discussion started by: rmanke
5 Replies

4. UNIX for Dummies Questions & Answers

Advanced grep'in... grep for data next to static element.

I have a directory I need to grep which consists of numbered sub directories. The sub directory names change daily. A file resides in this main directory that shows which sub directories are FULL backups or INCREMENTAL backups. My goal is to grep the directory for the word "full" and then... (2 Replies)
Discussion started by: SysAdm2
2 Replies

5. Shell Programming and Scripting

grep for certain files using a file as input to grep and then move

Hi All, I need to grep few files which has words like the below in the file name , which i want to put it in a file and and grep for the files which contain these names and move it to a new directory , full file name -C20091210.1000-20091210.1100_SMGBSC3:1000... (2 Replies)
Discussion started by: anita07
2 Replies

6. UNIX for Dummies Questions & Answers

| help | unix | grep (GNU grep) 2.5.1 | advanced regex syntax

Hello, I'm working on unix with grep (GNU grep) 2.5.1. I'm going through some of the newer regex syntax using Regular Expression Reference - Advanced Syntax a guide. ls -aLl /bin | grep "\(x\)" Which works, just highlights 'x' where ever, when ever. I'm trying to to get (?:) to work but... (4 Replies)
Discussion started by: MykC
4 Replies

7. UNIX for Dummies Questions & Answers

Append file with grep output but add timestamp?

I've setup a cron job that greps a file every five minutes and then writes (appends) the grep output/result to another file: grep "monkey" zoo.log | tail -1 >> cron-zoo-log Is there any way I can add the date and time (timestamp) to the cron-zoo-log file for each time a new line was added? ... (12 Replies)
Discussion started by: Sepia
12 Replies

8. Shell Programming and Scripting

MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else

Hi Guys, I need to set the value of $7 to zero in case $7 is NULL. I've tried the below command but doesn't work. Any ideas. thanks guys. MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else { print $7}}' ` Harby. (4 Replies)
Discussion started by: hariza
4 Replies

9. Shell Programming and Scripting

append o/p of grep in colomns

Hi, I am having a huge list of files out which i m just grepping files with extension .txt. For that i am using: ls |grep .txt And I am getting the desired o/p. But my o/p is too long (100 of files with .txt extension) thgat its going out of screen, like follow: a.txt b.txt dg.txt... (2 Replies)
Discussion started by: bisla.yogender
2 Replies

10. Shell Programming and Scripting

append a string to a grep result

hello, iostat -En | grep Vendor | grep -v DV | awk '{print $1 $2}' | sort -u returns Vendor:HP I want to append Disk to it. i.e.: Disk Vendor:HP how to do that? thanks (8 Replies)
Discussion started by: melanie_pfefer
8 Replies
Login or Register to Ask a Question