Solved

sed / awk / grep on files in directory

Posted on 2009-03-28
3
681 Views
Last Modified: 2013-12-26

Hello:
I need some help please. I need a ksh script that will perform some rather basic sed/awk grep commands.

I have a directory full of .txt files. Each file in this directory needs to undergo a treatment.
(learned from earlier EE posting)

1. sed -e 's/~/~\n/g' $file1 > $file1.$$

I'm interested in lines starting with "W12" within these files.  When I grep for that pattern in each .txt file, it produces STANDARDIZED output like this. (Delimiter will always be a '*' and position is fixed.)

W12*CL**18****VN*4921427~
W12*CL**3****VN*4921537~
W12*CL**4****VN*4931037~
W12*CS**2****VN*5057600~
W12*CS**5****VN*5057604~

2. I'd like to see the filename _AND_ line number where the grep match took place
3. Also need to count the # of times 'CL' and 'CS' appear in that output shown above.
4. Need to put a tab between the 5th and 6th char in the 7 digit integer shown in lines above.
5. Need to e-mail the ultimate output to a list of users

Thank you very much.

0
Comment
Question by:cjb123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 24011666
#matched lines
grep -n ^W12 $file1.$$


CL_count=`grep -n ^W12 $file1.$$ | grep CL | wc -l`
CS_count=`grep -n ^W12 $file1.$$ | grep CS | wc -l`

#4. Need to put a tab between the 5th and 6th char in the 7 digit integer shown in lines above.
grep -n ^W12 $file1.$$ | sed 's/\(.*\*\)\(.....\)\(..\)/\1\2\t\3/'   > final_out.txt

#5. Need to e-mail the ultimate output to a list of users
mutt -a final_out.txt  -s "the output" receiver@domain.com
0
 

Author Comment

by:cjb123
ID: 24013020
Sunnycoder:
thanks for quick reply.  Would you please help me get the FILENAME and LINE NUMBER from the grep commands where pattern "W12" isfound?  Thank you.
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 total points
ID: 24013147
Filename
grep -n ^W12 $file1.$$ | awk -F: ' { print $1 } '

Line Number
grep -n ^W12 $file1.$$ | awk -F: ' { print $2 } '

File name and line number
grep -n ^W12 $file1.$$ |  awk -F: ' { print $1 " " $2 } '

0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
canBalance challenge 34 114
noX challenge 17 144
Is there a simple front-end menu system. 9 148
I could not set window to top 4 63
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question