Sponsored Content
Top Forums Shell Programming and Scripting In a file, replace blank line by the last line not blank above Post 302992415 by bagvian on Friday 24th of February 2017 02:33:30 PM
Old 02-24-2017
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:
Code:
123
234

543


111
234


198

I would like this extract to be replaced by:
Code:
123
234
234
543
543
543
111
234
234
234
198
198

I wrote a simple script to do the job that reads myfile.txt and writes the result in out.txt:
Code:
MEMVAL=""
LINENUM=0
while read LINE
do
  LINENUM=$(($LINENUM+1));

  # Test if the line is blank
  CURLINE=`echo $LINE | grep -E '^[:space]*$'`
  TEST="$?"
  if [ $TEST == 0 ]; then 
    if [ $LINENUM == 1 ]; then # The first line cannot be blank!!
      exit 1
    else
      # Replace current blank line by memorised info in the output data file
      echo "$MEMVAL" >> out.txt
    fi
  else
    MEMVAL="$LINE"

    # At first init output data file
    if [ $LINENUM == 1 ]; then 
      echo "$LINE" > out.txt
    fi
  fi
done < myfile.txt

This script does the job, but it takes ages.
Also I guess there are far more efficient ways to do this by using either sed of awk (maybe csvfix in my case).

Would anybody have a suggestion?
Cheers, Bagvian


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 02-24-2017 at 03:56 PM.. Reason: Added CODE tags.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how to delete a first blank line from the file

I have a file which has the first blank line: sundev22$cat /t1/bin/startallocs /t1/bin/startallocsys 123 sundev22$ Is there a command to remove this first blank line? Thanks for help -A (4 Replies)
Discussion started by: aoussenko
4 Replies

2. Shell Programming and Scripting

ignoring blank line in a file

i have a file called Cleaner1.log . This files have some blank lines also.My requirement is that it should ignore the blank lines and give me the lines that contain some data. I m using this logic in a script: below the contents of file : Maximum Time Taken for Processing(Failed) RR... (4 Replies)
Discussion started by: ali560045
4 Replies

3. Shell Programming and Scripting

Remove last blank line of file

I have a number of files (arranged in directories) which have last line blank, I am trying to synchronize my code with other env and due to this blank lines, all files error out as different although only difference is that of balnk line at end of file. Is there a way I can recursively... (2 Replies)
Discussion started by: ruchimca
2 Replies

4. Shell Programming and Scripting

how to get the blank line number of a file?

I want to get the blank line number of a file. example: 9000|9000|WW|1|1|SL|472|472|LC|2272|1072|MTY|niceDay 9000|9000|WW|1|1|SL|470|470|MC|1270|1172|MPVT|nice 9000|9000|WW|1|1|SL|472|472|LC|1072|1672|MBD|Sonice 9000|9000|WW|1|1|SL|473|473|LF|1173|1173|MTY|nice666 I want to get... (5 Replies)
Discussion started by: robbiezr
5 Replies

5. Shell Programming and Scripting

how to replace a line in file with blank line

Hi I nned cmd to which will help me to replace a line in file with blank line e.g. file1 a b c d e after running cmd I shud get file1 b c d e (5 Replies)
Discussion started by: tarunn.dubeyy
5 Replies

6. Shell Programming and Scripting

Replace two blank line with a single blank line

Hi Guys, I have a file in which each set of records are separated by two blank line. I want to replace it with a single blank line. Can you guys help me out? Regards, Magesh (9 Replies)
Discussion started by: mac4rfree
9 Replies

7. Shell Programming and Scripting

Fill the empty line by adding line before blank line

FIle A "A" 2 aa 34 3 ac 5 cd "B" 3 hu 67 4 fg 5 gy output shud be A"" 2 aa 34 "A" 3 ac 34 "A" 5 cd 34 "B" 3 hu 67 "B" 4 fg 67 "B" 5 gy 67 (6 Replies)
Discussion started by: cdfd123
6 Replies

8. Shell Programming and Scripting

replace blank line number

hlow all i need help how can i replace blank number with awk input.txt 300::|355264313178490 301::|358814003239510 302::|358316038113400 303::|357954002633660 304::|354072040694090 305::|356956015214190 306::|352943020525180 307::|359574033836610 308::|381810990023580 so will be like... (4 Replies)
Discussion started by: zvtral
4 Replies

9. Shell Programming and Scripting

Need help to replace a pattern with a blank line

Need help to replace the line beginning with tcp_sendspace with a blank line. # cat if en0: flags=1e080863,480<UP,BROADCAST,NOTRAILERS,RUNNING,SIMPLEX,MULTICAST,GROUPRT,64BIT,CHECKSUM_OFFLOAD(ACTIVE),CHAIN> inet 10.27.53.21 netmask 0xffffff00 broadcast 10.207.52.255 inet... (11 Replies)
Discussion started by: sags007_99
11 Replies

10. UNIX for Beginners Questions & Answers

Deleting the last non blank line from a file

PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21 PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21 PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21 PGW|PGW_CDR_|2017-06-23 141946|2017-07-17 131633|2017-08-21 PGW|PGW_CDR_|2017-06-23 141946|2017-07-17... (6 Replies)
Discussion started by: swathi reddy1
6 Replies
MONEY_FORMAT(3) 							 1							   MONEY_FORMAT(3)

money_format - Formats a number as a currency string

SYNOPSIS
string money_format (string $format, float $number) DESCRIPTION
money_format(3) returns a formatted version of $number. This function wraps the C library function strfmon(3), with the difference that this implementation converts only one number at a time. PARAMETERS
o $format - The format specification consists of the following sequence: oa % character ooptional flags ooptional field width ooptional left precision ooptional right precision oa required conversion character Flags One or more of the optional flags below can be used: o = f - The character = followed by a (single byte) character f to be used as the numeric fill character. The default fill character is space. o ^ - Disable the use of grouping characters (as defined by the current locale). o + or ( - Specify the formatting style for positive and negative numbers. If + is used, the locale's equivalent for + and - will be used. If ( is used, negative amounts are enclosed in parenthesis. If no specification is given, the default is +. o ! - Suppress the currency symbol from the output string. o - - If present, it will make all fields left-justified (padded to the right), as opposed to the default which is for the fields to be right-justified (padded to the left). Field width o w - A decimal digit string specifying a minimum field width. Field will be right-justified unless the flag - is used. Default value is 0 (zero). Left precision o # n - The maximum number of digits ( n) expected to the left of the decimal character (e.g. the decimal point). It is used usually to keep formatted output aligned in the same columns, using the fill character if the number of dig- its is less than n. If the number of actual digits is bigger than n, then this specification is ignored. If group- ing has not been suppressed using the ^ flag, grouping separators will be inserted before the fill characters (if any) are added. Grouping separators will not be applied to fill characters, even if the fill character is a digit. To ensure alignment, any characters appearing before or after the number in the formatted output such as currency or sign symbols are padded as necessary with space characters to make their positive and negative formats an equal length. Right precision o . p - A period followed by the number of digits ( p) after the decimal character. If the value of p is 0 (zero), the decimal character and the digits to its right will be omitted. If no right precision is included, the default will dictated by the current local in use. The amount being formatted is rounded to the specified number of digits prior to formatting. Conversion characters o i - The number is formatted according to the locale's international currency format (e.g. for the USA locale: USD 1,234.56). o n - The number is formatted according to the locale's national currency format (e.g. for the de_DE locale: EU1.234,56). o % - Returns the % character. o $number - The number to be formatted. RETURN VALUES
Returns the formatted string. Characters before and after the formatting string will be returned unchanged. Non-numeric $number causes returning NULL and emitting E_WARNING. NOTES
Note The function money_format(3) is only defined if the system has strfmon capabilities. For example, Windows does not, so money_for- mat(3) is undefined in Windows. Note The LC_MONETARY category of the locale settings, affects the behavior of this function. Use setlocale(3) to set to the appropriate default locale before using this function. EXAMPLES
Example #1 money_format(3) Example We will use different locales and format specifications to illustrate the use of this function. <?php $number = 1234.56; // let's print the international format for the en_US locale setlocale(LC_MONETARY, 'en_US'); echo money_format('%i', $number) . " "; // USD 1,234.56 // Italian national format with 2 decimals` setlocale(LC_MONETARY, 'it_IT'); echo money_format('%.2n', $number) . " "; // Eu 1.234,56 // Using a negative number $number = -1234.5672; // US national format, using () for negative numbers // and 10 digits for left precision setlocale(LC_MONETARY, 'en_US'); echo money_format('%(#10n', $number) . " "; // ($ 1,234.57) // Similar format as above, adding the use of 2 digits of right // precision and '*' as a fill character echo money_format('%=*(#10.2n', $number) . " "; // ($********1,234.57) // Let's justify to the left, with 14 positions of width, 8 digits of // left precision, 2 of right precision, withouth grouping character // and using the international format for the de_DE locale. setlocale(LC_MONETARY, 'de_DE'); echo money_format('%=*^-14#8.2i', 1234.56) . " "; // Eu 1234,56**** // Let's add some blurb before and after the conversion specification setlocale(LC_MONETARY, 'en_GB'); $fmt = 'The final value is %i (after a 10%% discount)'; echo money_format($fmt, 1234.56) . " "; // The final value is GBP 1,234.56 (after a 10% discount) ?> SEE ALSO
setlocale(3), sscanf(3), sprintf(3), printf(3), number_format(3). PHP Documentation Group MONEY_FORMAT(3)
All times are GMT -4. The time now is 06:33 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy