Create two line from single line in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create two line from single line in shell
# 1  
Old 11-02-2007
Create two line from single line in shell

I wanted crate two lines by parsing one lin by exracting certian fields

I have record line somthing like below (Origanal line before Parsing)

DF02000XXYYYY00200.89ZZPPPP00800

Field discription :
DF - FirstField (Character Position 1 to 2)
02000 - Second Field (Characters position 3 to 7)
XX third Field (Characters position 8 to 9)
YYYY Fourth Field (Characters position 10 to 13)
00200.89 fifth Field (Characters position 14 to 21)
ZZ sixth Field (Characters position 22 to 23)
PPPP seventh Field (Characters position 24 to 27)
00800 eightth Field (Characters position 28 to 32)

I wanted to create two new line by parsing above line (each line having again 7 fields)
1st line should loolk line
DF0120000000000000000000000000000
DF - First Field (Same as that of origanal iine)
01200 - Second Field ( This is Difference between 2ndfiled and 8th Field of origanla line)

All other should be populated with 0's

2nd Line should look like
DF00800XXYYYY00200.89ZZ000000000

DF - First Field (Same as that of origanal iine)
00800 - Second Field ( This is same as that of the 8th Field of Origanal line)
XX - thrid field same as that of origanal line
YYYY - fourth field Same as that of the origanal line
00200.89 - Fifth field same as that of orignal line
All other fields should be zeros

If you still some clarification .. I will write back.
Thanks for all your help.

Thanks
Uni
# 2  
Old 11-02-2007
nawk -f uni.awk myFile

uni.awk:
Code:
#
function setFieldsByWidth(   i,n,start,copyd0) {
  # Licensed under GPL Peter S Tillier, 2003
  # NB corrupts $0
  copyd0 = $0                             # make copy of $0 to work on
  if (length(FIELDWIDTHS) == 0) {
    print "You need to set the width of the fields that you require" | stderr
    print "in the variable FIELDWIDTHS (NB: Upper case!)" | stderr
    exit(1)
  }

  if (!match(FIELDWIDTHS,/^[0-9 ]+$/)) {
    print "The variable FIELDWIDTHS must contain digits, separated" | stderr
    print "by spaces." | stderr
    exit(1)
  }

  n = split(FIELDWIDTHS,FWS)

  if (n == 1) {
    print "Warning: FIELDWIDTHS contains only one field width." | stderr
    print "Attempting to continue." | stderr
  }

  start = 1
  for (i=1; i <= n; i++) {
    $i = substr(copyd0,start,FWS[i])
    start = start + FWS[i]
  }
}

#I then call setFieldsByWidth() in my main awk code as follows:

BEGIN {
  FIELDWIDTHS="2 5 2 4 8 2 4 5" # for example
  stderr="cat 1>&2"
}
!/^[  ]*$/ {
  saveDollarZero = $0 # if you want it later
  setFieldsByWidth()
  formatF2= "%0" FWS[2] "d"
  formatREST= "%0" length(saveDollarZero) - FWS[1] - FWS[2] "d" "\n"
  # now we can manipulate $0, NF and $1 .. $NF as we wish
  $2=sprintf(formatF2, $2 - $8)
  printf("%s", $1 $2)
  printf(formatREST, 0)
  # second line
  $2=sprintf(formatF2, $8)
  printf("%s", $1 $2 $3 $4 $5)
  formatREST= "%0" length(saveDollarZero) - FWS[1] - FWS[2] - FWS[3] - FWS[4] - FWS[5] "d" "\n"
  printf(formatREST, 0)
}


Last edited by vgersh99; 11-02-2007 at 02:42 PM.. Reason: forgot the SECOND line
# 3  
Old 11-03-2007
Thanks this helps a lot

Thanks for your reply.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multi line log files to single line format

I want to read the log file which was generate from other command . And the output was having multi line in log files for job name and server name. But i need to make all the logs on one line Source file 07/15/2018 17:02:00 TRANSLOG_1700 Server0005_SQL ... (2 Replies)
Discussion started by: ranjancom2000
2 Replies

2. UNIX for Beginners Questions & Answers

Output to file print as single line, not separate line

example of problem: when I echo "$e" >> /home/cogiz/file.txt result prints to file as:AA BB CC I need it to save to file as this:AA BB CC I know it's probably something really simple but any help would be greatly appreciated. Thank You. Cogiz (7 Replies)
Discussion started by: cogiz
7 Replies

3. UNIX for Dummies Questions & Answers

To find and display the middle line in a file using single line command.

Hi all, How can i display the middle line of a file using a single line command? (6 Replies)
Discussion started by: Lakme Pemmaiah
6 Replies

4. UNIX for Dummies Questions & Answers

Remove multi line and single line comments

Hi, I am trying to remove multi line and single line comments like examples below I have tried this pattern. it works fine for single line comments and multi line comments in a single line only. but this fails when the comments are extended in multiple lines as shown in the comment 2 of... (3 Replies)
Discussion started by: ahmedwaseem2000
3 Replies

5. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

6. Shell Programming and Scripting

Shell script - Replace just part of a single line in a file.....

Hey guy's.... I new here, But im working on a school project, and I am not really good at programming. In fact, this is the only programming class that I need because programming is not what I am majoring in. But I have everything done in this shell script except for this last part..... ... (9 Replies)
Discussion started by: hxdrummerxc
9 Replies

7. Shell Programming and Scripting

Formatting File having big single line into 95 Char Per Line

Hi All, I have 4 big files which contains one big line containing formatted character records, I need to format each file in such way that each File will have 95 Characters per line. Last line of each file will have newline character at end. Before:- File Name:- File1.dat 102 121340560... (10 Replies)
Discussion started by: lancesunny
10 Replies

8. Shell Programming and Scripting

Joining multi-line output to a single line in a group

Hi, My Oracle query is returing below o/p ---------------------------------------------------------- Ins trnas value a lkp1 x a lkp1 y b lkp1 a b lkp2 x b lkp2 y ... (7 Replies)
Discussion started by: gvk25
7 Replies

9. Shell Programming and Scripting

single line input to multiple line output with sed

hey gents, I'm working on something that will use snmpwalk to query the devices on my network and retreive the device name, device IP, device model and device serial. I'm using Nmap for the enumeration and sed to clean up the results for use by snmpwalk. Once i get all the data organized I'm... (8 Replies)
Discussion started by: mitch
8 Replies

10. Shell Programming and Scripting

Reading Multiple Variables From a Single Line in Shell

I'm a Linux newb, I've been running a Debian Linux server for about a year now, and I've written some simple scripts to automate various things, but I still don't know much, and I forget what I learn as fast as I figure it out... Anyway, that really isn't important, I just want you to know that... (14 Replies)
Discussion started by: Drek
14 Replies
Login or Register to Ask a Question