The UNIX and Linux Forums  

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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Formatted Output dhanamurthy Shell Programming and Scripting 6 05-12-2008 11:30 PM
Formatted output - awk dhanamurthy Shell Programming and Scripting 3 05-11-2008 08:25 PM
abt viewing all the columns in the output as it is.. RRVARMA UNIX for Advanced & Expert Users 1 03-13-2008 07:46 AM
formatted output with commas joeyg Shell Programming and Scripting 4 03-04-2008 11:54 AM
Formatted output in KSH psynaps3 Shell Programming and Scripting 1 07-05-2006 05:03 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-16-2008
Read Only
 

Join Date: Jun 2006
Posts: 105
cut - columns with formatted Output

Hi
I have the input file as below
Code:
***TEST10067
00567GROSZ 099
00567CTCTSDS90
***TEST20081
08233GROZWEWE
00782GWERW899
***TEST30088
08233GROZWEWE
00782GWERW899
I am finding the lines starting with *** and outputing as below
Code:
TEST10067
TEST20081
TEST30088
I need a space between TEST1 and 0067 and similarly for the other records.

grep ^\* 100109.C|cut -c 4-8,' ',9-12
I tried with the above. Is there anyway to format the output with a space.
as i am not getting the desired results.

Regards
Dhana

Last edited by Yogesh Sawant; 05-17-2008 at 02:27 AM. Reason: added code tags
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-16-2008
jaduks's Avatar
Registered User
 

Join Date: Aug 2007
Location: Assam,India
Posts: 141
Code:
$ cat dh.txt
***TEST10067
00567GROSZ 099
00567CTCTSDS90
***TEST20081
08233GROZWEWE
00782GWERW899
***TEST30088
08233GROZWEWE
00782GWERW899

$ sed -n '/^\*/p' dh.txt
***TEST10067
***TEST20081
***TEST30088

$ sed -n '/^\*/p' dh.txt | sed 's/\*\*\*\([A-Z]*\)\([0-9]*\)/\1 \2/'
TEST 10067
TEST 20081
TEST 30088
//Jadu
Reply With Quote
  #3 (permalink)  
Old 05-17-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,084
Might as well condense it into a single sed script.

Code:
sed -n '/^*/!d;s/\*\*\*\([A-Z]*\)\([0-9]*\)/\1 \2/p' dh.txt
Reply With Quote
  #4 (permalink)  
Old 05-18-2008
Read Only
 

Join Date: Jun 2006
Posts: 105
cut - output in formatted way

Hi
I got the output as below

$ sed -n '/^*/!d;s/\*\*\*\([A-Z]*\)\([0-9]*\)/\1 \2/p' filename

TEST1 0067*01
TEST2 0081*02
TEST3 0088*03

I need only
TEST1 0067
TEST2 0081
TEST3 0088

and not *01,*02 and *03. I am trying it out.
Meanwhile is there any other simplest way of doing it.

Regards
Dhana
Reply With Quote
  #5 (permalink)  
Old 05-18-2008
rubin's Avatar
Registered User
 

Join Date: Nov 2007
Posts: 151
Code:
awk -F"TEST" '/^\*/{split($2,a,"");print FS a[1], a[2]a[3]a[4]a[5]}' filename
Reply With Quote
  #6 (permalink)  
Old 05-18-2008
Registered User
 

Join Date: Jan 2008
Posts: 7
ruby one liner

If your input is indeed fixed width, here's a one-liner in ruby:
Code:
$ ruby -e 'STDIN.readlines.each { |l| puts  "#{l[3..7]} #{l[8..-1]}" if l[0..2] == "***"  }' <filename
And the same sort of thing in python:
Code:
python -c 'import sys; print "\n".join([ "%s %s" % (l[3:7], l[8:-1]) for l in sys.stdin.readlines() if l[0:3] == "***" ])' <filename
Or in awk
Code:
awk '/^\*\*\*/ { print(substr($1,4,5),  substr($1,9,10)); }' <filename
--
Q
Reply With Quote
  #7 (permalink)  
Old 05-18-2008
Read Only
 

Join Date: Jun 2006
Posts: 105
Hi
The awk looks good to me and i tried changing on it

awk '/^\*\*\*/ { printf "%s %s\n",substr($1,4,5), substr($1,9,4) }' < filename

if my file has the following inputs

***BRRAA0067**
TESTSS
sdfasdf
***SIZZ 0081**
sdfas
sdfasd
***TYPEE0078**
dsfas
asdfasdf

I am getting the below output

BRRAA 0067
SIZZ
TYPEE 0078

If you note that SIZZ has only four characters and a space then i am getting the output as shown above. But i need 0081 also coming up after SIZZ .

Do any one of you have some idea on what needs to be changed ?

REgards
Dhana
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 08:26 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0