Splitting file based on pattern and first character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Splitting file based on pattern and first character
# 1  
Old 05-25-2012
Splitting file based on pattern and first character

I have a file as below
pema.txt


Code:
s2dhshfu dshfkdjh dshfd 
rjhfjhflhflhvflxhvlxhvx vlvhx
sfjhldhfdjhldjhjhjdhjhjxhjhxjxh
sjfdhdhfldhlghldhflhflhfhldfhlsh
rjsdjh#error occured#
skjfhhfdkhfkdhbvfkdhvkjhfvkhf
sjkdfhdjfh#error occured#

my requirement is to create 3 files frm the above file

1) pema.junk
will contain all the records where the txt #error occured# is present
2) pema.s
should contain all record starting with s except one's in pema.junk
3)pema.r
should contain all records starting with r except one's in pema.junk


The first line of the file doesnt contain any dta so should be ignored

and if possible all data after 135 position should be truncatd when copyin
into s and r file

Last edited by joeyg; 05-25-2012 at 09:52 AM.. Reason: Please wrap data and scripts with CodeTags
# 2  
Old 05-25-2012
Like this..?
Code:
awk '/#error occured#/ {printf ("%.135s\n",$0) > "pema.junk"} /^r/ && ! /#error occured#/ { printf ("%.135s\n",$0) > "pema.r"} /^s/ && ! /#error occured#/ {printf ("%.135s\n",$0) > "pema.s"}' pema.txt

I know it looks dirty Smilie
This User Gave Thanks to clx For This Post:
# 3  
Old 05-25-2012
It doesn't have to. Here's the same code formatted:

Code:
awk '
  /#error occured#/ {
    printf ("%.135s\n",$0) > "pema.junk"
  }

  /^r/ && ! /#error occured#/ { 
    printf ("%.135s\n",$0) > "pema.r"
  } 

  /^s/ && ! /#error occured#/ {
    printf ("%.135s\n",$0) > "pema.s"
  }
' pema.txt

These 2 Users Gave Thanks to Scott For This Post:
# 4  
Old 05-25-2012
Or:
Code:
awk '{f="pema." (/#error occured#/?"junk":substr($0,1,1)); print substr($0,1,135)>f}' infile

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 05-28-2012
thank you guys.. will try and let you know

---------- Post updated at 12:52 AM ---------- Previous update was at 12:42 AM ----------

i tried it but i'm getting an error
syntax error The source line is 13.
The error context is
>>> pema. <<< txt

---------- Post updated at 01:14 AM ---------- Previous update was at 12:52 AM ----------

apologies, the code works perfectly... the error was due to me..thank you very much

---------- Post updated at 01:24 AM ---------- Previous update was at 01:14 AM ----------

a slight modification on the below code
Code:
awk '
  /#error occured#/ {
    printf ("%.135s\n",$0) > "pema.junk"
  }

  /^r/ && ! /#error occured#/ { 
    printf ("%.135s\n",$0) > "pema.r"
  } 

  /^s/ && ! /#error occured#/ {
    printf ("%.135s\n",$0) > "pema.s"
  }
' pema.txt

can we make it so that the S and R is ignored and also if the record is shorted than 135 can we pad it with spaces so that its 135 in length?

Last edited by Scrutinizer; 05-28-2012 at 03:25 AM.. Reason: code tags
# 6  
Old 05-28-2012
Do you mean ignore the case of r or s? So that line that start with S and R are directed to the same file? Something like this?
Code:
awk '
  function pr(f){
    printf ("%-135s\n",substr($0,1,135))>f
  }     
                  
  /#error occured#/ {
    pr("pema.junk")
    next
  }

  /^(r|R)/ { 
    pr("pema.r")
  } 

  /^(s|S)/ {
    pr("pema.s")
  }
' infile


--
Some awks can do this:
Code:
awk '{NF=135; f="pema." (/#error occured#/?"junk":tolower($1)); print>f}' FS= infile

# 7  
Old 05-28-2012
sorry for the confusion, i mean to to say , can i ignore 'S' and 'R'
fo that the record looks like
Code:
2dhshfu dshfkdjh dshfd

instead of
Code:
s2dhshfu dshfkdjh dshfd


Last edited by Scrutinizer; 05-28-2012 at 04:33 AM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Splitting a file based on a pattern

Hi All, I am having a problem. I tried to extract the chunk of data and tried to fix I am not able to. Any help please Basically I need to remove the for , values after K, this is how it is now A,, B, C,C, D,D, 12/04/10,12/04/10, K,1,1,1,1,0,3.0, K,1,1,1,2,0,4.0,... (2 Replies)
Discussion started by: arunkumar_mca
2 Replies

2. Shell Programming and Scripting

Splitting textfile based on pattern and name new file after pattern

Hi there, I am pretty new to those things, so I couldn't figure out how to solve this, and if it is actually that easy. just found that awk could help:(. so i have a textfile with strings and numbers (originally copy pasted from word, therefore some empty cells) in the following structure: SC... (9 Replies)
Discussion started by: luja
9 Replies

3. Shell Programming and Scripting

Splitting based on occurence of a Character at fixed position

I have a requirement where i need to split a file based on occurence of a character which is present at a fixed position. Description is as below: 1. The file will be more than 1 Lakh records. 2. Each line will be of fixed length of 987 characters. 3. At position 28 in each line either 'C' or... (9 Replies)
Discussion started by: Neelkanth
9 Replies

4. Shell Programming and Scripting

Merging two special character separated files based on pattern matching

Hi. I have 2 files of below format. File1 AA~1~STEVE~3.1~4.1~5.1 AA~2~DANIEL~3.2~4.2~5.2 BB~3~STEVE~3.3~4.3~5.3 BB~4~TIM~3.4~4.4~5.4 File 2 AA~STEVE~AA STEVE WORKS at AUTO COMPANY AA~DANIEL~AA DANIEL IS A ELECTRICIAN BB~STEVE~BB STEVE IS A COOK I want to match 1st and 3rd... (2 Replies)
Discussion started by: crypto87
2 Replies

5. Shell Programming and Scripting

File character adjustment based on specific character

i have a reqirement to adjust the data in a file based on a perticular character the sample data is as below 483PDEAN CORRIGAN 52304037528955WAGES 50000 89BP ABCD MASTER352 5434604223735428 4200 58BP SOUTHERN WA848 ... (1 Reply)
Discussion started by: pema.yozer
1 Replies

6. Shell Programming and Scripting

Problem with splitting large file based on pattern

Hi Experts, I have to split huge file based on the pattern to create smaller files. The pattern which is expected in the file is: Master..... First... second.... second... third.. third... Master... First.. second... third... Master... First... second.. second.. second..... (2 Replies)
Discussion started by: saisanthi
2 Replies

7. Shell Programming and Scripting

Splitting large file into multiple files in unix based on pattern

I need to write a shell script for below scenario My input file has data in format: qwerty0101TWE 12345 01022005 01022005 datainala alanfernanded 26 qwerty0101mXZ 12349 01022005 06022008 datainalb johngalilo 28 qwerty0101TWE 12342 01022005 07022009 datainalc hitalbert 43 qwerty0101CFG 12345... (19 Replies)
Discussion started by: jimmy12
19 Replies

8. UNIX for Dummies Questions & Answers

Splitting a file based on first 8 chars

I have an input file of this format <Date><other data> For example, 20081213aaaaaaaaa 20081213bbbbbbbbb 20081220ccccccccc 20081220ddddddddd 20081220eeeeeeeee 20081227ffffffffffffff The first 8 chars are date in YYYYMMDD formT. I need to split this file into n files where n is the... (9 Replies)
Discussion started by: paruthiveeran
9 Replies

9. Shell Programming and Scripting

Splitting a file based on two patterns

Hi there, I've an input file as follows: *START 1001 a1 1002 a2 1003 a3 1004 a4 *END *START 1001 b1 1002 b2 1004 b4 *END *START 1001 c1 1004 c4 *END (6 Replies)
Discussion started by: kbirde
6 Replies

10. Shell Programming and Scripting

Splitting the file based on logic

Hello I have a requirement where i need to split the Input fixed width file which contains multiple invoices into multiple files with 2 invoices per file. Each invoice can be identified by its first line's second character which is "H" and sixth character is " " space and the invoice would... (10 Replies)
Discussion started by: dsdev_123
10 Replies
Login or Register to Ask a Question