Split multiple blank, even if it's blank


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split multiple blank, even if it's blank
# 1  
Old 03-16-2016
Split multiple blank, even if it's blank

Hello,

I am trying to split :
Code:
qvipari1           opcmsga     OVO Message Agent                   AGENT,EA              Aborted
qvipbcarls02           opcmsga     OVO Message Agent                   AGENT,EA              Aborted
qvipcac3           perfalarm   Alarm generator                                           Stopped
qvipbdc10           opcmsga     OVO Message Agent                   AGENT,EA              Aborted

I don't know how to awk but i need for example :
$1 = qvipari1
$2 = opcmsga
$3 = OVO Message Agent
$4 = AGENT,EA
$5 = Aborted

(i use | while read LINE on my file, but i can't distinct multiple blank to one blank ... )

Do you know how i can do that ?

Thanks,

Last edited by cterra; 03-16-2016 at 01:17 PM..
# 2  
Old 03-16-2016
Not sure I understand correctly what you're after. Given you have multiple spaces between fields and just single spaces within fields, you could try setting the field separator to multiple spaces:
Code:
awk -F"  +" '{print NF}' file
5
5
4
5

You can't, of course, detect empty fields, then.
# 3  
Old 03-16-2016
I am trying something like that :
Code:
cat $mylog | while read LINE
do
	#echo $LINE
	 SERVER2USE=$(echo $LINE | awk -F'           ' '{ print $1 }')
	 Process=$(echo $LINE | awk -F'           ' '{ print $2 }')
	 Description=$(echo $LINE | awk -F'           ' '{ print $3 }')
	 Type=$(echo $LINE | awk -F'           ' '{ print $4 }')
	 Status=$(echo $LINE | awk -F'           ' '{ print $5 }')
	
         #some interpretation ...
         echo $Status
done

But my awk doesn't understand multiple blank space delimiter :'(

Last edited by cterra; 03-16-2016 at 01:36 PM..
# 4  
Old 03-16-2016
awk does it by default:
Code:
 SERVER2USE=$(echo "$LINE" | awk '{ print $1 }')
 Process=$(echo "$LINE" | awk '{ print $2 }')

But more efficient is
Code:
while read SERVER2USE Process Description Type Status junk
do
  echo "$Status"
done < $mylog

This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 03-17-2016
it doesn't work, that's why i am posted here, i am not totally noob.
It give me :
Code:
SERVER2USE = qvipari1
Process =
Description =OVO
Type = Message
Status = Agent
junk = AGENT,EA              Aborted

command "read", make a variable every blank, but i need to cut every 6 blanks ...

same for awk ...

And more difficult, i can have empty variable so i need my script to see 12 blanks as 2 variables with one empty ...

Maybe it was not clear cause of the original identation, i ve modify my log, to be more comprehensible
Code:
#server        #process      #description                            #type             #status
qvipar           opcmsga     OVO Message Agent                   AGENT,EA              Aborted
qvipbc           opcmsga     OVO Message Agent                   AGENT,EA              Aborted
qvipca           perfalarm   Alarm generator                                           Stopped
qvipbd           opcmsga     OVO Message Agent                   AGENT,EA              Aborted

i think
Code:
awk -F"  +" '{print NF}' file

is close tio what i need, but i still don't understand it, i ve just seen it sometimes during my research

Last edited by cterra; 03-17-2016 at 10:55 AM..
# 6  
Old 03-17-2016
Try introducing tabs:
Code:
unexpand -t "18 30 66 88" file | awk -F'\t' '{print $1,$2,$3,$4,$5}'


---
With GNU awk 4 you can use field widths:
Code:
awk -v FIELDWIDTHS="17 12 36 22 20" '{print $1, $2, $3, $4, $5}' file

But then the fields contain spaces that need to be stripped//
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 03-17-2016
It's absolutely non-trivial to deal with empty fields when field separators are multiple spaces, and your sample does NOT have six spaces as separators but any number, some six or larger. Try
Code:
awk -F"  +" '
NR == 1         {gsub (/#/, _)
                 n = split ($0, HD)
                 next
                }
NF < 5          {$(NF+1) = $NF
                 $(NF-1) = ""
                }
                {for (i=1; i<=n; i++) print HD[i], "=", $i
                }

' file
server = qvipar
process = opcmsga
description = OVO Message Agent
type = AGENT,EA
status = Aborted
server = qvipbc
process = opcmsga
description = OVO Message Agent
type = AGENT,EA
status = Aborted
server = qvipca
process = perfalarm
description = Alarm generator
type = 
status = Stopped
server = qvipbd
process = opcmsga
description = OVO Message Agent
type = AGENT,EA
status = Aborted

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split file when value in column is blank

Hi Experts, In short : Need to split file when field in column 5 is blank and need to generate two file in which column 5 is blank and other in which column 5 has values along with other rows and column data My issue is i am not able to get header for column from raw file into new file which... (4 Replies)
Discussion started by: as7951
4 Replies

2. Shell Programming and Scripting

In a file, replace blank line by the last line not blank above

Dear All, In a CSV file, say that a given column has been extracted. In that column, information is missing (i.e. blank lines appear). I would like to replace the blank lines by the last valid line (not blank) previously read. For example, consider the extract below: 123 234 543 111... (7 Replies)
Discussion started by: bagvian
7 Replies

3. Shell Programming and Scripting

To check Blank Lines, Blank Records and Junk Characters in a File

Hi All Need Help I have a file with the below format (ABC.TXT) : ®¿¿ABCDHEJJSJJ|XCBJSKK01|M|7348974982790 HDFLJDKJSKJ|KJALKSD02|M|7378439274898 KJHSAJKHHJJ|LJDSAJKK03|F|9898982039999 (cont......) I need to write a script where it will check for : blank lines (between rows,before... (6 Replies)
Discussion started by: chatwithsaurav
6 Replies

4. UNIX for Advanced & Expert Users

Delete blank spaces and blank lines in a file

Hi Gurus, Somebody can say me how to delete blank spaces and blank lines in a file unix, please. Thank you for advanced. (10 Replies)
Discussion started by: systemoper
10 Replies

5. UNIX for Dummies Questions & Answers

Split file based on number of blank lines

Hello All , I have a file which needs to split based on the blank lines Name ABC Address London Age 32 (4 blank new line) Name DEF Address London Age 30 (4 blank new line) Name DEF Address London (8 Replies)
Discussion started by: Pratik4891
8 Replies

6. Shell Programming and Scripting

Delete blank lines, if blank lines are more than one using shell

Hi, Consider a file named "testfile" The contents of file are as below first line added for test second line added for test third line added for test fourth line added for test fifth line added for test (5 Replies)
Discussion started by: anil8103
5 Replies

7. Shell Programming and Scripting

how to append blank line in multiple files in unix

Dear All- Please suggest a single unix command which can append blank line in multiple files. I need to achieve this using a single unix command and not a script Please do advice Regards, Suresh (2 Replies)
Discussion started by: sureshg_sampat
2 Replies

8. Shell Programming and Scripting

PERL: removing blank lines from multiple files

Hi Guru's , I have a whole bunch of files in /var/tmp that i need to strip any blank lines from, so ive written the following script to identify the lines (which works perfectly).. but i wanted to know, how can I actually strip the identified lines from the actual source files ?? my... (11 Replies)
Discussion started by: hcclnoodles
11 Replies

9. Shell Programming and Scripting

multiple search separated by blank line

hi all I want to get multiple search that are separated by blank line according to first search in text file eg: egrep 'x|Y|Z' file in that for each x...it should be seperated by blank line will u pls help (8 Replies)
Discussion started by: suryanarayana
8 Replies

10. Shell Programming and Scripting

regex to delete multiple blank lines in a file?

can't figure out a way to delete multiple empty lines but keep single empty lines in a file, file is like this #cat file 1 2 3 4 5 6 - What I want is 1 2 (6 Replies)
Discussion started by: fedora
6 Replies
Login or Register to Ask a Question