Fill the empty line by adding line before blank line


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Fill the empty line by adding line before blank line
# 1  
Java Fill the empty line by adding line before blank line

FIle A
Code:
"A"  2  aa  34
     3  ac
      5  cd 
"B"  3   hu  67
      4  fg
      5  gy

output shud be

Code:
A"" 2  aa  34
 "A"   3   ac 34
 "A"   5  cd 34 
"B"  3   hu  67
"B"   4  fg  67
 "B"  5  gy  67

# 2  
something like this.

previous_filed.pl
Code:
#!/usr/bin/perl

while (<>) {
        if (/(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/) {
                $pr_1=$1;
                $pr_4=$4;
                print $_;
        }
        else
        {
                chomp;
                print $pr_1,$_,"\t",$pr_4,"\n";
        }

}

invocation
Code:
perl previous_filed.pl inputfile

This User Gave Thanks to pravin27 For This Post:
# 3  
Hi

Code:
#awk 'NF>2{a=$1;b=$4;$1=$1;print;next;}{$1=$1;print a,$0,b;}' file
"A" 2 aa 34
"A" 3 ac 34
"A" 5 cd 34
"B" 3 hu 67
"B" 4 fg 67
"B" 5 gy 67

Guru.
These 2 Users Gave Thanks to guruprasadpr For This Post:
# 4  
With awk:
Code:
awk 'NF==4 {a=$1; b=$4; print; next} {print a, $0, b}' infile
"A"  2  aa  34
"A"      3  ac 34
"A"       5  cd  34
"B"  3   hu  67
"B"       4  fg 67
"B"       5  gy 67

If you want it a bit more properly aligned, you might want to use printf instead of print.
This User Gave Thanks to zaxxon For This Post:
# 5  
perl may help you

Code:
my ($a,$b);
while(<DATA>){
  chomp;
  if(/^(".*?").* (\S*)$/){
   $a=$1;
  $b=$2;
  print $_,"\n";
  }
  else{
   print $a," ",$_," ",$b,"\n";
  }
}
__DATA__
"A"  2  aa  34
     3  ac
      5  cd
"B"  3   hu  67
      4  fg
      5  gy

This User Gave Thanks to summer_cherry For This Post:
# 6  
Quote:
Originally Posted by guruprasadpr
Hi

Code:
#awk 'NF>2{a=$1;b=$4;$1=$1;print;next;}{$1=$1;print a,$0,b;}' file
"A" 2 aa 34
"A" 3 ac 34
"A" 5 cd 34
"B" 3 hu 67
"B" 4 fg 67
"B" 5 gy 67

Guru.
Nice.
# 7  
Another awk aprox:

Code:
awk '{if ( /"/){a=$1;b=$NF}else{$0=a""$0" "b}}1' file

This User Gave Thanks to Klashxx For This Post:
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #345
Difficulty: Medium
Scott McNealy received $238 million USD when the Oracle acquisition closed.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding a blank line in between two O/Ps in tabular format which gets received over email

Hi Guys, I am stuck in between and seeking help here. Requirement: A script that will run every morning which will connect to Mysql database and run the query to inform us about the holidays (it will also check if there were any holidays during last 2 business days). So the three queries are... (11 Replies)
Discussion started by: Sambit Sahu
11 Replies

2. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

3. Shell Programming and Scripting

String search and print next all lines in one line until blank line

Dear all I want to search special string in file and then print next all line in one line until blank lines come. Help me plz for same. My input file and desire op file is as under. i/p file: A1/EXT "BSCABD1_21233G1" 757 130823 1157 RADIO X-CEIVER ADMINISTRATION BTS EXTERNAL FAULT ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

4. Shell Programming and Scripting

Delete the last empty/blank line of the text file

Hi All, I have a file.txt which seems like having three lines. wc -l file.txt 3 file.txt In fact, once it is open in text editor, this file has four lines where the last line is empty. how can i delete this last empty line of the file.txt? I tried the codes below so far but they... (6 Replies)
Discussion started by: senayasma
6 Replies

5. Shell Programming and Scripting

sed adding a blank line

I use the following as part of a script to correct for a faulty hostname file. # get the domain name read -r thehostname < /etc/hostname dom="$(echo $thehostname | cut -d'.' -f2)" numchar=${#dom} if then echo "It appears as though the hostname is not correctly set." echo "Hostname has... (5 Replies)
Discussion started by: bugeye
5 Replies

6. Shell Programming and Scripting

how to scan for the empty or blank line

I am having a text file sample.txt The contents of the sample.txt is as follows. ____________ aaa,bbb ccc,ddd ____________ first line contains aaa,bbb second line is blank third line contains ccc,ddd Now I need to write a script to search in the file sample.txt. If it contains... (4 Replies)
Discussion started by: kmanivan82
4 Replies

7. Shell Programming and Scripting

Adding New empty line in a file

Hi, I am new to Sed /awk commands. using that i want to add new empty line after every line in a file by leaving first three lines. So, can any one help me out how to achieve this. Example: --------- Input Filename: file1.txt Input Data: --------Report-------- Date:20-10-03... (4 Replies)
Discussion started by: G.K.K
4 Replies

8. Shell Programming and Scripting

Adding a blank line after every 5th line

Hello... I have a file which contain certain number of records. I want to generate another file from this file which will contain 1st line as a blank line & after every 5 lines one blank line will be inserted. How to achieve this through shell scripting? Thanks... (5 Replies)
Discussion started by: 46019
5 Replies

9. Shell Programming and Scripting

SED - adding blank line after each Range Match

the following range matching works great but i wish to add a blank line after each range result set... which i've tried and researched to no avail MY INPUT DATA: CURRENT CODE I'M USING: sed -n '/*$/,/;/p' $INPUT_FILE RESULTS I'M GETTING: RESULT I looking to... (5 Replies)
Discussion started by: danmauer
5 Replies

10. UNIX for Dummies Questions & Answers

adding blank line in egrep

I followed the egrep example given in the thread "parse text or complex grep ?". It is exactly what I need...except... how do I insert a blank line after the second line? My exact command is: egrep 'patt1|patt2' filename the result is: patt1 patt2 patt1 patt2 and so on. I would... (2 Replies)
Discussion started by: antalexi
2 Replies

Featured Tech Videos