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
Changing one column of delimited file column to fixed width column manneni prakash Shell Programming and Scripting 5 06-22-2009 05:27 AM
how to read the column and print the values under that column gemini106 Shell Programming and Scripting 6 03-28-2008 07:05 AM
How to check Null values in a file column by column if columns are Not NULLs Mandab Shell Programming and Scripting 7 03-15-2008 09:57 AM
How to pass one parameter from one column to another column in AWK prasanta jena Shell Programming and Scripting 1 07-30-2007 06:08 AM
Replace 10th column with a new column--- Terriblly hurry ahmedwaseem2000 Shell Programming and Scripting 2 09-06-2005 01:10 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-07-2008
SUMNIX SUMNIX is offline
Registered User
  
 

Join Date: Aug 2008
Location: Mumbai
Posts: 3
Column scattered onto multiple new lines

Hi All,

I am getting a pipe separated flat file which is to be loaded into the database. The issue is that the last column of the record is scattered onto multiple new lines. Ideally one record should be on a single line ( Please refer the last three lines of the attached flat file) but in this case the flat file generation utility is not generating it correctly and I have to live with this problem

Will really appreciate if anybody would be able to give some solution to fix this issue.

Thanks in advance.
Attached Files
File Type: txt flat_file.txt (876 Bytes, 20 views)

Last edited by SUMNIX; 08-07-2008 at 08:29 AM.. Reason: incomplete subject line
  #2 (permalink)  
Old 08-07-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Your input lines are split by CRLF (probably the file has been generated on Windows).
The below Awk script outputs something similar to what you want on my Linux box
(you should use nawk or /usr/xpg4/bin/awk if you want to use the code on Solaris):

Code:
awk -F\| 'END { print _ }
NF > 1 && _ { print  _ ; _ = "" }
{ _ = _ ? _ $0 : $0 }
' RS='\r\n' ORS='\r\n' flat_file.txt
Logically, the spaces that are missing in your input will not be present in the output too (SAINDAS instead of SAIN DAS).

If you're running awk on Windows, you should use a script file, rather than a command.

If the above code does not work (because you saved the sample on Windows, modifying the record separator),
just try it without setting the RS and ORS variables.
  #3 (permalink)  
Old 08-07-2008
SUMNIX SUMNIX is offline
Registered User
  
 

Join Date: Aug 2008
Location: Mumbai
Posts: 3
I am running it on Solaris and used xpg4 version of awk to execute the below awk script. I have removed the RS and ORS variables cause they were not helping in output file generation.


awk -F\| 'END { print _ }
NF > 1 && _ { print _ ; _ = "" }
{ _ = _ ? _ $0 : $0 } flat_file.txt
The output file which i got is still not the correct one.

The original file records are having 9 fields separated by "|"
I am running the awk which is creating one more records at the start and it is putting the scattered column charaters at the begining of the record. In short it is creating one more record at the beginning and in some cases it is loosing the characters. Please observe the second record in the attached output file which I got after running the above awk.

Please help in generating the correct flat file.

Thanks...
Attached Files
File Type: txt output_file.txt (839 Bytes, 10 views)
  #4 (permalink)  
Old 08-07-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Quote:
Originally Posted by SUMNIX View Post
[...]
Please help in generating the correct flat file.
Could you please run:

Code:
dos2unix flat_file.txt flat_file_unix.txt
And then rerun the code I posted with the correct file - flat_file_unix.txt. I suppose you'll get the correct result.
  #5 (permalink)  
Old 08-08-2008
SUMNIX SUMNIX is offline
Registered User
  
 

Join Date: Aug 2008
Location: Mumbai
Posts: 3
I tried the solution given by radoulov and it worked perfectly this time.

I have also tried the sudhamacs awk solution on the original file and is also worked as expected.

Thanks a lot guy's
  #6 (permalink)  
Old 08-07-2008
sudhamacs sudhamacs is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 98
Assuming that the start of the line will be "DAT|"

awk -F'|' '$1=="DAT"{printf "\n" }; {printf $0 ;}' flat_file.txt
Closed Thread

Bookmarks

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 08:48 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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