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
# 8  
Old 03-18-2016
Quote:
Originally Posted by RudiC
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

I ve done something like your code, thanks, it was first time i did loop in awk :
Code:
	tmp=""
	
	SERVER2USE=$(echo $LINE | awk -F' ' '{ print $1 }')
	Process=$(echo $LINE | awk -F' ' '{ print $2 }')
	tmp=$(echo $LINE | awk -F' ' '
	{	
		i = 3 
		do { 
				print $i
 
				i++
			} while(i < NF-1) 
	
	}
	
	')
	Type=$(echo $LINE | awk -F' ' '{ print $(NF-1) }')
	Status=$(echo $LINE | awk -F' ' '{ print $NF }')
	Description=$(echo "$tmp" | tr '\n' ' ')
	echo "SERVER2USE = $SERVER2USE"
	echo "Process = $Process"
	echo "Description = ${Description}"
	echo "Type = $Type"

# 9  
Old 03-18-2016
Sure that it works? The type becomes "generator" in your third record, the one with the empty field.

And, you are creating 16 processes instead of 1 when working with a single awk solution.
This User Gave Thanks to RudiC For This Post:
# 10  
Old 03-18-2016
Quote:
Originally Posted by RudiC
Sure that it works? The type becomes "generator" in your third record, the one with the empty field.

And, you are creating 16 processes instead of 1 when working with a single awk solution.
I ve all my processus, my final code is :
Code:
cat $logpathGetSysInfo_ITO_Process_Status.sh-$site-$env.log | while read LINE
do
	tmp=""
	SERVER2USE=$(echo $LINE | awk -F' ' '{ print $1 }')
	Process=$(echo $LINE | awk -F' ' '{ print $2 }')
	tmp=$(echo $LINE | awk -F' ' '
	{	
		i = 3 
		do { 
				print $i
 
				i++
			} while(i < NF-1) 
	}
	
	')
	Type=$(echo $LINE | awk -F' ' '{ print $(NF-1) }')
	Status=$(echo $LINE | awk -F' ' '{ print $NF }')
	Description=$(echo "$tmp" | tr '\n' ' ')
	if [  "$Status" = "Aborted" ] || [  "$Status" = "Stopped" ]  ; then
		echo "$SITE;$ENV;$SERVER2USE;ITO;Processus;Critical;$Status;$Process : ${Description}"
	fi
done

The end line will push it to database through the file generated by the output

But you re right, i was so focus on my first "do while" into a "awk", that i ve forgotten the type ...

Now i understand better awk i will try to implement your code, it seems to be the perfect answer, it was just to hard for me and my first awk do while.
# 11  
Old 03-18-2016
With awk instead of the shell loop, you can make use of a multi-space field separator (as RudyC suggested already)
Code:
awk -F '  +' '($NF~/Aborted|Stopped/) {print SITE ";" ENV ";" $1 ";ITO;Processus;Critical;" $NF ";" $2 " : " $(NF-1)}' SITE="$SITE" ENV="$ENV" file

---------- Post updated at 01:56 PM ---------- Previous update was at 01:29 PM ----------

Here is a variant that can cope with the empty TYPE field
Code:
awk -F '  +' '{print SITE ";" ENV ";" $1 ";ITO;Processus;Critical;" $NF ";" $2 " : " $3 " : " ((NF>4) ? $4 : "")}' SITE="$SITE" ENV="$ENV" file


Last edited by MadeInGermany; 03-18-2016 at 03:35 PM..
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