Help with shell script to extract certain information


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with shell script to extract certain information
# 1  
Old 11-13-2014
Java Help with shell script to extract certain information

Hi,
I have a file which I need to programmatically split into two files.

All the information in the file before pattern "STOP HERE" is to be stripped and output into one file while everything after "STOP HERE" is to be output into a separate file.

I would appreciate help on how to do this please.

thank you.
# 2  
Old 11-13-2014
You can use sed for achieving this.
Let us know what you have tried, thanks :-)
# 3  
Old 11-13-2014
Unfortunately I didn't even know where to start. I was reading up on egrep and I've been having a look at sed as you suggested. If you could throw some more light, I'd appreciate it.

thank you
# 4  
Old 11-13-2014
Try
Code:
awk '/STOP HERE/ {FN="XX"; next} {print > FN}' FN="YY" file

# 5  
Old 11-13-2014
Code:
# get stuff before string from file and redirect to file1
sed '/STOP HERE/,$d' file > file1

Code:
# get stuff after string from file and redirect to file2
sed -n '/STOP HERE/,$ {/STOP HERE/!p}' file > file2

For correct output, the source file should have only 1 STOP HERE string.

Btw. personally I prefer this (verbose Smilie) awk for the "before stuff", don't know how to invert it though Smilie:
Code:
awk '!/STOP HERE/ {print} /STOP HERE/ {exit}' file > file1


Last edited by junior-helper; 11-15-2014 at 02:49 PM.. Reason: 2 typos in comments for sed commands; s/from file1/from file/
# 6  
Old 11-15-2014
Thank you both, much appreciated.
Code:
awk '/STOP HERE/ {FN="XX"; next} {print > FN}' FN="YY" file

Please could you explain the actions? FN, I can't seem to find that as an awk variable. Please could you expantiate a bit.

Code:
awk '!/STOP HERE/ {print} /STOP HERE/ {exit}' file > file1

Everything that is not 'STOP HERE' in the input file print but exit at 'STOP HERE' and output to file1, is this what this is doing? I will try this and see how it goes.

thank you again.

Last edited by Franklin52; 11-17-2014 at 07:01 AM.. Reason: Please use code tags
# 7  
Old 11-15-2014
Quote:
Originally Posted by PTL
is this what this is doing?
Exactly.

1. Read line by line from file
2. String STOP HERE *not* present in the line? -> print line to file1 (actually print to terminal/stdout, but due to stream redirection (>file1) it's printed to file1.
3. String STOP HERE present in the line? -> exit
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk script to extract transcript information from gff3 file

I need help to extract transcript information from gff3 file. Here is the input Chr01 JGI gene 82773 86941 . - . ID=Potri.001G000900;Name=Potri.001G000900 Chr01 JGI mRNA 82793 86530 . - . ID=PAC:27047814;Name=Potri.001G000900.1;pacid=27047814;longest=1;Parent=Potri.001G000900... (6 Replies)
Discussion started by: Maduranga
6 Replies

2. Shell Programming and Scripting

Shell script, sftp logfile not showing transfer information

Hello, Recently I have changed some crontab scripts which I execute to do different tasks mainly transfering some files from one server to the other. The main change was the protocol from ftp transfer to sftp. Ftp server was the MS Windows default service, and Sftp server is an proprietary sw... (4 Replies)
Discussion started by: enux
4 Replies

3. Shell Programming and Scripting

shell script to collect information from current and remote unix boxes

I am using AIX 5.3.0.0 ; I need a script to find out each remote AIX boxes Hostname, Model name and Serial number in following format Hostname Modelname SerialID AIXMC01 IBM,7026-B80 IBM,0110BBA1F AIXMC02 IBM,7026-H50 IBM,0110BBA56 AIXMC03 IBM,7026-H50 IBM,0110BBARR... (12 Replies)
Discussion started by: amir07
12 Replies

4. Shell Programming and Scripting

Make shell script get information from the internet

Hello, I am attempting to learn shell for approx. two weeks. So, sorry if I am writing wrong things. Can a shell script get some information for itself from a site? I can't post the site right know, I don't have privileges. But it can be any site that shows the currency between whatever.... (3 Replies)
Discussion started by: alpa8le
3 Replies

5. Shell Programming and Scripting

Create shell script to extract unique information from one file to a new file.

Hi to all, I got this content/pattern from file http.log.20110808.gz mail1 httpd: Account Notice: close igchung@abc.com 2011/8/7 7:37:36 0:00:03 0 0 1 mail1 httpd: Account Information: login sastria9@abc.com proxy sid=gFp4DLm5HnU mail1 httpd: Account Notice: close sastria9@abc.com... (16 Replies)
Discussion started by: Mr_47
16 Replies

6. Shell Programming and Scripting

Disk Monitoring shell script giving incorrect information

Hi All, OS: Linux 86x64 bits Red Hat Linux I get the email alert for the following when Alert condition is set for 30: /dev/sda1 99M 21M 74M 22% /boot -> Below 30%(Should not get the email alert) Expected output as per E-Mail alert: /dev/sda3 20G ... (2 Replies)
Discussion started by: a1_win
2 Replies

7. Shell Programming and Scripting

shell script to sort information in one file

Hi to all, anyway to create shell script to sort informations from one file and create new file with the sorted values? from file 30days.out -bash-3.00# more 30days.out user/str4@kl.com/INBOX user/tg1@johor.com/INBOX user/tg2@kedah.com/INBOX user/tg3@titangroup.com/INBOX... (3 Replies)
Discussion started by: Mr_47
3 Replies

8. Shell Programming and Scripting

Shell script for user login information.

Hi Gurus, I need help in writing a script which should say which user has used or logged in in the server from past one month using FTP or TELNET and the output should be of the form Username Service NumberofTimes Date. Thanks in Advance. ---------- Post updated at 04:01 PM... (1 Reply)
Discussion started by: rama krishna
1 Replies

9. Homework & Coursework Questions

2. Write a shell script that produces some summary information of the system at a particular moment

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 2. Relevant commands, code, scripts, algorithms: 3. The attempts at a solution (include all... (2 Replies)
Discussion started by: jsk319342
2 Replies

10. UNIX for Dummies Questions & Answers

Write a script to extract information from a db

Hi I need to put together a script that will search certain tables in a db and send that data to a csv file. Basically I am importing data to a db and I want to write a script to check that all information was imported correctly. Thank you (1 Reply)
Discussion started by: ladyAnne
1 Replies
Login or Register to Ask a Question