Bash script to send lines of file to new file based on Regex

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script to send lines of file to new file based on Regex
# 1  
Old 05-05-2013
Bash script to send lines of file to new file based on Regex

I have a file that looks like this:

cat includes

What I want to do is make three new files with just those selections. So the three lowercase lines under CORP-CRASHTEST-BU would output to another file, then CORP-TEST would also output the following three lowercase lines to another file. I have tried to use arrays, but if I have to provide numbers to identify the arrays it would be hard since it is not just the file above but other files that I want to perform this operation on. I need a a script that would send anything underneath a line that contains capital letters to another file. If anyone has a suggestion on how to start on this, it would be appreciated. I have not started on it yet, so I'm just asking for a general idea.
# 2  
Old 05-05-2013
There's no three lines under CORP-CRASHTEST-BU. Please carefully match your written specification with your sample data. Nevertheless, try
$ awk '$0==toupper($0){fn=$0} {if (fn) print >fn}' file

# 3  
Old 05-05-2013
Not much different approach:
awk '$0~/[[:upper:]]/{if(F) close(F);F=$0;next}{print > F}' includes

This User Gave Thanks to Yoda For This Post:
# 4  
Old 05-05-2013
The difference is that Yoda's approach would fire a new file name if $0 contains one single upper case char anywhere, while the other needs an all upper $0. The specification is a bit vague in this point...
This User Gave Thanks to RudiC For This Post:
# 5  
Old 05-06-2013
Thanks to all

I found what I needed after checking your examples:

gawk '/[A-Z0-9.-]$/{x="F"++i;}{print > x;}'

This works like a charm and I got four files F1 F2 F3 and F4 which are correctly split as I desired!

Without your help I never would have looked into this and your examples have encouraged me to learn even more about awk/gawk/nawk.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Creating bash script to process a data file based on the menu

Hey, im fairly new to unix and Im trying to make this unix project that would display a menu and do the following. MENU =========================== (p, P) Print users info (a, A) Add new user (s, S) Search user (d, D) Delete user (x,X) Exit Enter your choice: Trying to... (3 Replies)
Discussion started by: ultimaxtrd
3 Replies

2. Shell Programming and Scripting

Bash to copy file 3 times and rename based on another file

In the below bash I am trying to copy the only text file (always only one) in /home/cmccabe/Desktop/list/QC/metrics.txt and rename each of the 3 text files according to /home/cmccabe/Desktop/test/list.txt using lines 3, 4 ,5. This format (that is list.txt) is always 5 lines. Thank you :). ... (12 Replies)
Discussion started by: cmccabe
12 Replies

3. Shell Programming and Scripting

Bash to search file based off user input then create new file

In the below bash a file is downloaded when the program is opened and then that file is searched based on user input and the result is written to a new file. For example, the bash is opened and the download.txt is downloaded, the user then enters the id (NA04520). The id is used to search... (5 Replies)
Discussion started by: cmccabe
5 Replies

4. Shell Programming and Scripting

Bash script - printing range of lines from text file

I'm working on a new exercise that calls for a script that will take in two arguments on the command line (representing the range of line numbers) and will subsequently print those lines from a a specified file. Command line would look like this: ./lines_script.bash 5 15 <file.txt. The script would... (8 Replies)
Discussion started by: ksmarine1980
8 Replies

5. Shell Programming and Scripting

Help with ksh-to read ip file & append lines to another file based on pattern match

Hi, I need help with this- input.txt : L B white X Y white A B brown M Y black Read this input file and if 3rd column is "white", then add specific lines to another file insert.txt. If 3rd column is brown, add different set of lines to insert.txt, and so on. For example, the given... (6 Replies)
Discussion started by: prashob123
6 Replies

6. Shell Programming and Scripting

bash script to find date based on search string for continuesly updating file

Hi All, I am very new to UNIX and I have tried this for a longtime now and unable to crack it.... There is a file that is continuously updating. I need to search for the string and find the date @ which it updated every day..... eg: String is "work started" The log entry is as below: ... (1 Reply)
Discussion started by: Nithz
1 Replies

7. Shell Programming and Scripting

regex matches from lines in file

Hello, I try to script something (bash-script) and can not find a way to get and store a match into a variable from a line in a file. grep isn't useful as the matches are not returned - just colored. I can't get 'expr' to work for me. Is it necessary to use a perl-script with regex instead? ... (7 Replies)
Discussion started by: daWonderer
7 Replies

8. Shell Programming and Scripting

Problem in splitiing file based on regex using awk/nawk

I have a file tmp.txt as shown below: Controller: 0 Disk: 0.0.0 Disk: 0.1.0 Disk: 0.2.0 Controller: 1 Volume:c1t2d0 Disk: 0.0.0 Disk: 0.1.0 Disk: 0.2.0 Controller: 2 Volume:c2t2d0 Disk: 0.2.0 Now I want to split... (4 Replies)
Discussion started by: durbam2002
4 Replies

9. Shell Programming and Scripting

Read the lines within file and send each line to different new file

I need help.....I have number of data files and I need to read the contains of each file which is three lines by awk commands or script, and send each line within the data file to different new file . That means I will have three files as a result, the first file will contains the first lines for... (4 Replies)
Discussion started by: aldreho
4 Replies

10. Shell Programming and Scripting

Bash script to delete folder based on text file information

I have been working on a script to list all the name's of a subfolder in a text file then edit that text file and then delete the subfolder base on the edited text file so far I have been able to do every thing I just talked about but can't figure out how to delete the subfolers base on a text file... (8 Replies)
Discussion started by: bone11409
8 Replies
Login or Register to Ask a Question

Featured Tech Videos