Sponsored Content
Top Forums Shell Programming and Scripting Shell script to merge and delete lines Post 303026590 by Jag02 on Friday 30th of November 2018 05:00:30 AM
Old 11-30-2018
Hi,
I am using Linux operating system and using bash shell.
The } are part of the input file and cannot be changed as i need them in the output file as well.
The blocks i want to merge is not always located adjacent to each other. They can be positioned at any line number in the file.
Eq Name and Id are fixed strings

I have tried with the following code and i am stuck on how to move the lines and delete the block.
Code:
#cat -n file
 1  POLY_STATS
     2  {
     3  EqName 103_tri
     4  Id 123
     5  act_polyCount 1
     6  act_polyValue 0
     7  }
     8  POLY_STATS
     9  {
    10  EqName 103_tri
    11  Id 123
    12  pass_polyCount 2
    13  pass_polyValue 0
    14  }
    15  POLY_STATS
    16  {
    17  EqName 103
    18  Id 123
    19  mes_polyCount 2
    20  mes_polyValue 0
    21  }
    22  POLY_STATS
    23  {
    24  EqName 504
    25  Id 123
    26  mes_polyCount 2
    27  mes_polyValue 0
    28  }
    29  }

Code:
 sort file | uniq -c | grep EqName|gawk '$1==2{print $3}' #Get the value of Eqname,which is repeating twice
 cat -n file|sed -n '/103_tri/p' #get line number where EqName is coming twice
 From=$(cat -n file|sed -n '/103_tri/p'|awk '{print $1}'|tail -1)
 echo $From #10
 From=$(echo "$From + 2" | bc)
 To=$(echo "$From + 1" | bc)
 sed -n $From,${To}p file # Printing the lines from the second block which i want to move to the first block

I have printed the lines to be moved
Code:
pass_polyCount 2
pass_polyValue 0

Now i want to insert the above two lines after line no 6 and delete the lines from 8 to 14. Pl help
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

shell script to merge files

Can anybody help me out with this problem " a shell program that takes one or any number of file names as input; sorts the lines of each file in ascending order and displays the non blank lines of each sorted file and merge them as one combined sorted file. The program generates an error... (1 Reply)
Discussion started by: arya
1 Replies

2. Shell Programming and Scripting

Need Shell Script to delete lines in a file

Hi All, I have a file with 3 columns (Bank Name, Account Number and Amount). My requirement, I need to delete lines using Unix Shell script: 1. Which are having Alphanumeric characters in Account Number (eg. Line3). 2. Which are having 0.00 in amount. (eg. Line4) 3. And also I need to... (4 Replies)
Discussion started by: phani333
4 Replies

3. Shell Programming and Scripting

Help with merge Shell Script

I have a file test.log The content of the file is : a:R_yz:1 a:R_cd:2 a:F_bc:0 a:F_xx:3 b:R_dg:5 b:R_gf:1 b:F_fd:4 I want the output is : :a R_yz 1 3 3 : R_cd 2 : F_bc 0 : F_xx 3 :b R_dg 5 6 4 : R_gf 1 : F_fd 4 (8 Replies)
Discussion started by: mnmonu
8 Replies

4. Shell Programming and Scripting

remove blank lines and merge lines in shell

Hi, I'm not a expert in shell programming, so i've come here to take help from u gurus. I'm trying to tailor a csv file that i got to make it work for the LOAD FROM command. I've a datatable csv of the below format - --in file format xx,xx,xx ,xx , , , , ,,xx, xxxx,, ,, xxx,... (11 Replies)
Discussion started by: dvah
11 Replies

5. Shell Programming and Scripting

How to select/delete some lines in shell?

I need to delete half(approx) the file or select half the file by existence of some character My file looks like 1 2 3 4 . . . 50 . . 100I need to select only 50 to rest of the file or needs to delete the file upto 50. Please help me out.. (6 Replies)
Discussion started by: SujeethP
6 Replies

6. UNIX for Dummies Questions & Answers

How to delete lines above a certin line number in bash shell

Hi, I have written a script that returns the line number of the pattern i want and i stored the line number in a variable(getlinenumber).Now i want to delete all the lines in a file above this line number which is stored in a variable. i am using sed '1,$getlinenumberd' > file1.txt which is... (2 Replies)
Discussion started by: learninguser235
2 Replies

7. Shell Programming and Scripting

How to merge Expect script inside shell script?

Hi I have two scripts one is Expect and other is shell. I want to merge Expect code in to Shell script so that i can run it using only one script. Can somebody help me out ? Order to execute: Run Expect_install.sh first and then when installation completes run runTests.sh shell script. ... (1 Reply)
Discussion started by: ashish_neekhra
1 Replies

8. Shell Programming and Scripting

Bash/shell merge similar lines

Hello, I've been working on a bash script to parse through firewall logs (cisco). I'm nearing the end and have a dilemma. My data looks as such (actual data is several gigs worth of logs - without the headers): sourceIP destinationIP destinationProtocol destinationPort 1.1.1.1 2.2.2.2 ... (2 Replies)
Discussion started by: umang2382
2 Replies

9. Shell Programming and Scripting

Delete all CONSECUTIVE text lines from file shell scripting

Hi I have a text file like below. THe content of the text will vary. Entire text file have four consecutive lines followed with blank line. I want to delete the occurrence of the two consicutive lines in the text file. I don't have pattern to match and delete. Just i need to delete all... (5 Replies)
Discussion started by: RJSKR28
5 Replies

10. UNIX for Beginners Questions & Answers

Merge multi-lines into one single line using shell script or Linux command

Hi, Can anyone help me for merge the following multi-line log which beginning with a " and line ending with ": into one line. *****Original Log***** 087;2008-12-06;084403;"mc;;SYHLR6AP1D\LNZW;AD-703;1;12475;SYHLR6AP1B;1.1.1.1;0000000062;HGPDI:MSISDN=12345678,APNID=1,EQOSID=365;... (3 Replies)
Discussion started by: rajeshlinux2010
3 Replies
TAIL(1) 						    BSD General Commands Manual 						   TAIL(1)

NAME
tail -- display the last part of a file SYNOPSIS
tail [-F | -f | -r] [-b number | -c number | -n number] [file ...] DESCRIPTION
The tail utility displays the contents of file or, by default, its standard input, to the standard output. The display begins at a byte, line or 512-byte block location in the input. Numbers having a leading plus (``+'') sign are relative to the beginning of the input, for example, ``-c +2'' starts the display at the second byte of the input. Numbers having a leading minus (``-'') sign or no explicit sign are relative to the end of the input, for example, ``-n 2'' displays the last two lines of the input. The default starting location is ``-n 10'', or the last 10 lines of the input. The options are as follows: -b number The location is number 512-byte blocks. -c number The location is number bytes. -f The -f option causes tail to not stop when end of file is reached, but rather to wait for additional data to be appended to the input. The -f option is ignored if the standard input is a pipe, but not if it is a FIFO. -F The -F option implies the -f option, but tail will also check to see if the file being followed has been renamed or rotated. The file is closed and reopened when tail detects that the filename being read from has a new inode number. The -F option is ignored if reading from standard input rather than a file. -n number The location is number lines. -r The -r option causes the input to be displayed in reverse order, by line. Additionally, this option changes the meaning of the -b, -c and -n options. When the -r option is specified, these options specify the number of bytes, lines or 512-byte blocks to display, instead of the bytes, lines or blocks from the beginning or end of the input from which to begin the display. The default for the -r option is to display all of the input. If more than a single file is specified, each file is preceded by a header consisting of the string ``==> XXX <=='' where ``XXX'' is the name of the file. DIAGNOSTICS
The tail utility exits 0 on success, and >0 if an error occurs. SEE ALSO
cat(1), head(1), sed(1) STANDARDS
The tail utility is expected to be a superset of the IEEE Std 1003.2-1992 (``POSIX.2'') specification. In particular, the -F, -b and -r options are extensions to that standard. The historic command line syntax of tail is supported by this implementation. The only difference between this implementation and historic versions of tail, once the command line syntax translation has been done, is that the -b, -c and -n options modify the -r option, i.e. ``-r -c 4'' displays the last 4 characters of the last line of the input, while the historic tail (using the historic syntax ``-4cr'') would ignore the -c option and display the last 4 lines of the input. HISTORY
A tail command appeared in PWB UNIX. BSD
June 6, 1993 BSD
All times are GMT -4. The time now is 02:51 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy