Ksh - finding pattern in file and generating a new file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ksh - finding pattern in file and generating a new file
# 1  
Old 01-09-2009
Ksh - finding pattern in file and generating a new file

I am trying to find for the pattern in first 5 bytes of every line in a text file and then generate a new file with the pattern found.

Example: expected pattern is '-' to be serached in first 5 bytes of file only.

Input File

ab-cd jan-09
ddddd jan09
cc-ww jan09
dsgdq jan-09

Output Required :

ab-cd jan-09
cc-ww jan09

Can anyone suggest how to achieve this using unix command or script?
# 2  
Old 01-09-2009
Quote:
Originally Posted by net
I am trying to find for the pattern in first 5 bytes of every line in a text file and then generate a new file with the pattern found.

Example: expected pattern is '-' to be serached in first 5 bytes of file only.

Input File

ab-cd jan-09
ddddd jan09
cc-ww jan09
dsgdq jan-09

Output Required :

ab-cd jan-09
cc-ww jan09

Can anyone suggest how to achieve this using unix command or script?
You can use grep to search for lines not having - in its first five positions and use -v option to print all other lines(i.e lines having - in first five chars)
Code:
$ cat t
ab-cd jan-09
ddddd jan09
cc-ww jan09
dsgdq jan-09
$ grep -v '^[^-]\{5\}' t
ab-cd jan-09
cc-ww jan09
$

# 3  
Old 01-09-2009
Hey Thanks :-) This is working.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding the file name in a directory with a pattern

I need to find the latest file -filename_YYYYMMDD in the directory DIR. the below is not working as the position is shifting each time because of the spaces between(occuring mostly at file size field as it differs every time.) please suggest if there is other way. report =‘ls -ltr... (2 Replies)
Discussion started by: archana25
2 Replies

2. Shell Programming and Scripting

Finding file pattern in ksh 88

Hi, I've to find the file which has the pattern "Delete Report for History Tables" and need to search this file pattern from directory which has sub directories as well. I'm using ksh 88 Please suggest me which command will be used to find the file pattern . Thanks. (1 Reply)
Discussion started by: smile689
1 Replies

3. Shell Programming and Scripting

Finding the pattern and replacing the pattern inside the file

i have little challenge, help me out.i have a file where i have a value declared and and i have to replace the value when called. for example i have the value for abc and ccc. now i have to substitute the value of value abc and ccc in the place of them. Input File: go to &abc=ddd; if... (16 Replies)
Discussion started by: saaisiva
16 Replies

4. 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

5. Shell Programming and Scripting

Finding duplicates in a file excluding specific pattern

I have unix file like below >newuser newuser <hello hello newone I want to find the unique values in the file(excluding <,>),so that the out put should be >newuser <hello newone can any body tell me what is command to get this new file. (7 Replies)
Discussion started by: shiva2985
7 Replies

6. Shell Programming and Scripting

[ksh] finding last file with keyword in it

Hi, In short : I have several log files and I need to find the last file with a certain keyword in it. # ls -1tr logs log_hostX.Jan01_0100.gz log_hostX.Jan01_0105.gz log_hostX.Jan01_0110.gz log_hostX.Jan01_0115.gz log_hostX.Jan01_0120.gz log_hostX.Jan01_0125.gz log_hostX.Jan01_0130.gz... (2 Replies)
Discussion started by: ejdv
2 Replies

7. Shell Programming and Scripting

Finding 4 current files having specific File Name pattern

Hi All, I am trying to find 4 latest files inside one folder having following File Name pattern and store them into 4 different variables and then use for processing in my shell script. File name is fixed length. 1) Each file starts with = ABCJmdmfbsjop letters + 7 Digit Number... (6 Replies)
Discussion started by: lancesunny
6 Replies

8. UNIX for Dummies Questions & Answers

Need help finding a file where a pattern exists and the file has a timestamp

So, I know how to do some of this stuff on an individual level, but I'm drawing a blank as to how to put it all together. I have a pattern that I'm looking for in a log file. The log file I know came in yesterday, so I want to limit the search to that day's listing of files. How would I do... (5 Replies)
Discussion started by: kontrol
5 Replies

9. Red Hat

Need help with a shell script for finding a certain pattern from log file

Hell Guys, Being a newbie, I need some help in finding a certain string from a log file of thousands of lines (around 30K lines) and have the output in a separate file. Below is the file output - 10.155.65.5 - - "POST... (15 Replies)
Discussion started by: rockf1bull
15 Replies

10. Shell Programming and Scripting

help with finding & replacing pattern in a file

Hi everyone. Could u be so kind and help me with on "simple" shell script? 1. i need to search a file line by line for a pattern. example of a lines in that file 2947 domain = feD,id = 00 0A 02 48 17 1E 1D 39 DE 00 0E 00,Name Values:snNo = f10 Add AttFlag = 0 2. i need to find... (0 Replies)
Discussion started by: dusoo
0 Replies
Login or Register to Ask a Question