The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
grep using date format ali560045 Shell Programming and Scripting 4 12-26-2007 08:59 AM
grep using date format ali560045 Shell Programming and Scripting 8 12-11-2007 06:39 PM
how i prepare a c++ code(c code) for implementing my own protocol format amitpansuria High Level Programming 1 09-06-2007 11:09 PM
how to format a grep command jasongr Shell Programming and Scripting 2 11-18-2005 02:07 PM
How to format number/string in ksh GNMIKE Shell Programming and Scripting 2 07-03-2005 03:44 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-01-2008
krishna.fuji krishna.fuji is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 5
Thumbs up Grep, then format then prepare a string

Hi

I have a file which is having line like below

Personal Unit=AU003 (Industrial Products Division),Plant=B00089,Departmant=D110

When ever i fine line starting sith Personal Unit and contains Plant Department I need to pick this line and format it like

Personal Unit=AU003
Plant=B00089
Departmant=D110
-->RAM:AU003:B00089110:system date:header of the file

i used sed

sed -e '/Personal Unit/{;h;s/^/#/p;x;}' -e '/Business Unit/{s/,Plant/\nPlant/g' -e 's/,Departmant/\nDepartmant/g'} FileName

But it is not fullfilling my all need
can any one please suggest
  #2 (permalink)  
Old 10-02-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,212
With awk you can do something like:
Code:
awk -F, '/^Personal Unit=/&&/Plant/&&/Departmant/{sub("\(.*\)","")}{print $1 OFS $2 OFS $3}' OFS="\n" file
Regards
  #3 (permalink)  
Old 10-02-2008
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 699
Hi.

I had to change the sub like so, from quote to slash:
Code:
#!/usr/bin/env sh

# @(#) user1    Demonstrate 2-level parsing with awk.

#  ____
# /
# |   Infrastructure BEGIN

set -o nounset
echo

## The shebang using "env" line is designed for portability. For
#  higher security, use:
#
#  #!/bin/sh -

## Use local command version for the commands in this demonstration.

set +o nounset
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) awk
set -o nounset

echo

FILE=${1-data1}
echo " Input file $FILE:"
cat $FILE

# Use nawk or /usr/xpg4/bin/awk on Solaris.

# |   Infrastructure END
# \
#  ---


echo
echo " Results from awk:"
awk -F, '
/^Personal Unit=/ && /Plant/ && /Departmant/ {sub(/\(.*\)/,"")}
        {print $1 OFS $2 OFS $3}
' OFS="\n" $FILE

exit 0
Producing:
Code:
% ./user1

(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
GNU Awk 3.1.4

 Input file data1:
Personal Unit=AU003 (Industrial Products Division),Plant=B00089,Departmant=D110

 Results from awk:
Personal Unit=AU003
Plant=B00089
Departmant=D110
cheers, drl
  #4 (permalink)  
Old 10-02-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,369
Another awk solution.
Code:
awk -F'[(|,|)]' '/^Personal/{printf "%s\n%s\n%s\n",$1,$4,$5}' file
  #5 (permalink)  
Old 10-03-2008
krishna.fuji krishna.fuji is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 5
its not the exact output what we expected

Quote:
Originally Posted by drl View Post
Hi.

I had to change the sub like so, from quote to slash:
Code:
#!/usr/bin/env sh
 
# @(#) user1    Demonstrate 2-level parsing with awk.
 
#  ____
# /
# |   Infrastructure BEGIN
 
set -o nounset
echo
 
## The shebang using "env" line is designed for portability. For
#  higher security, use:
#
#  #!/bin/sh -
 
## Use local command version for the commands in this demonstration.
 
set +o nounset
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) awk
set -o nounset
 
echo
 
FILE=${1-data1}
echo " Input file $FILE:"
cat $FILE
 
# Use nawk or /usr/xpg4/bin/awk on Solaris.
 
# |   Infrastructure END
# \
#  ---
 
 
echo
echo " Results from awk:"
awk -F, '
/^Personal Unit=/ && /Plant/ && /Departmant/ {sub(/\(.*\)/,"")}
        {print $1 OFS $2 OFS $3}
' OFS="\n" $FILE
 
exit 0
Producing:
Code:
% ./user1
 
(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
GNU Awk 3.1.4
 
 Input file data1:
Personal Unit=AU003 (Industrial Products Division),Plant=B00089,Departmant=D110
 
 Results from awk:
Personal Unit=AU003
Plant=B00089
Departmant=D110
cheers, drl


Hi I have multiple lines in this script. As soon as it fine the sentence immediatly it has to return the given out put. more over the below mentioned extra line it is not giving. I shall be very thankful if do so
  #6 (permalink)  
Old 10-03-2008
krishna.fuji krishna.fuji is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 5
Quote:
Originally Posted by krishna.fuji View Post
Hi I have multiple lines in this script. As soon as it fine the sentence immediatly it has to return the given out put. more over the below mentioned extra line it is not giving. I shall be very thankful if do so

[krishna@newtrans-test ~]$ cat ram1
Personal Unit=US003 (Industrial Products Division),Plant=B00089,Departmant=D110
This is my own
Personal Unit=US003 (Industrial/Products Division),Plant=B00089,Departmant=D110
We need to makfmkldfd
Personal Unit=US004 (Consumer Products Div)Plant=B00089,Departmant=D1119
mdkmvckldmldm
This is for some idea when junk of data in file
Personal Unit=US004 (Consumer Products Div),Plant=B00078,Departmant=D111
Personal Unit=US006 (Machinery Mfg Division),Plant=B00089,Departmant=D1188
Personal Unit=US007 (Adhesives Division) ,Plant=B00089,Departmant=D110
Personal Unit=US009 (Adhesives Division)
Personal Unit=US010 (Adhesives Division)

[krishna@newtrans-test ~]$ sed -e '/Personal Unit/{;h;s/^/#/p;x;}' -e '/Personal Unit/{s/,Plant/\nPlant/g' -e 's/,Departmant/\nDepartmant/g'} ram1
# Personal Unit=US003 (Industrial Products Division),Plant=B00089,Departmant=D110
Personal Unit=US003 (Industrial Products Division)
Plant=B00089
Departmant=D110
This is my own
# Personal Unit=US003 (Industrial/Products Division),Plant=B00089,Departmant=D110
Personal Unit=US003 (Industrial/Products Division)
Plant=B00089
Departmant=D110
We need to makfmkldfd
# Personal Unit=US004 (Consumer Products Div)Plant=B00089,Departmant=D1119
Personal Unit=US004 (Consumer Products Div)Plant=B00089
Departmant=D1119
mdkmvckldmldm
This is for some idea when junk of data in file
# Personal Unit=US004 (Consumer Products Div),Plant=B00078,Departmant=D111
Personal Unit=US004 (Consumer Products Div)
Plant=B00078
Departmant=D111
# Personal Unit=US006 (Machinery Mfg Division),Plant=B00089,Departmant=D1188
Personal Unit=US006 (Machinery Mfg Division)
Plant=B00089
Departmant=D1188
# Personal Unit=US007 (Adhesives Division) ,Plant=B00089,Departmant=D110
Personal Unit=US007 (Adhesives Division)
Plant=B00089
Departmant=D110
# Personal Unit=US009 (Adhesives Division)
Personal Unit=US009 (Adhesives Division)
# Personal Unit=US010 (Adhesives Division)
Personal Unit=US010 (Adhesives Division)
[krishna@newtrans-test ~]$

Along with this where ever i am splitting that row i need to get the combination of values
Like
--> RAM:US007:B00089110:systendate:fileheader
Conclusion is
wherever i find
Personal Unit=US007 (Adhesives Division) ,Plant=B00089,Departmant=D110
immediatly next line must be
Personal Unit=US007 (Adhesives Division) ,Plant=B00089,Departmant=D110
Personal Unit=US007 (Adhesives Division)
Plant=B00089
Departmant=D110

--> RAM:US007:B00089110:systendate:fileheader

and important is this script should touch only the line which are having combination of Personal Unit,Plant,Department in a single line
IF you can do this for me thanks alot

Last edited by krishna.fuji; 10-03-2008 at 02:29 AM..
  #7 (permalink)  
Old 10-03-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,369
Quote:
Originally Posted by krishna.fuji View Post
and important is this script should touch only the line which are having combination of Personal Unit,Plant,Department in a single line
Forget about fancy colors, and use code tags when you post code or data.
Code:
awk -F, '{if(/Personal Unit/&&/Plant/&&/Departmant/){printf "# %s\n%s\n%s\n%s\n",$0,$1,$2,$3}else{print}}' file
Sponsored Links
Closed Thread

Bookmarks

Tags
linux commands

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 10:59 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0