Text manipulation with sed - Advanced technic


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Text manipulation with sed - Advanced technic
# 1  
Old 08-07-2018
Text manipulation with sed - Advanced technic

Hello everybody,

I have the following input file:

Code:
START ANALYSIS 1
DATA LINE
DATA LINE
DATA LINE
DATA LINE
  Libray   /home/me/myLibrary
  Source   library_name_AAAAA
DATA LINE
DATA LINE
DATA LINE
  BEGIN SOURCE ANALYSIS
  Function A
  Function B
  Function C
  Function D
  END 4 Functions Founded
DATA LINE
DATA LINE
START ANALYSIS 2
DATA LINE
DATA LINE
DATA LINE
DATA LINE
  Libray   /home/me/myLibrary
  Source   library_name_BBBBB
DATA LINE
DATA LINE
DATA LINE
  BEGIN SOURCE ANALYSIS
  Function E
  Function F
  Function G
  Function H
  END 4 Functions Founded
DATA LINE
DATA LINE

This is the output file expected :

Code:
/home/me/myLibrary library_name_AAAAA Function A
/home/me/myLibrary library_name_AAAAA Function B
/home/me/myLibrary library_name_AAAAA Function C
/home/me/myLibrary library_name_AAAAA Function D
/home/me/myLibrary library_name_BBBBB Function E
/home/me/myLibrary library_name_BBBBB Function F
/home/me/myLibrary library_name_BBBBB Function G
/home/me/myLibrary library_name_BBBBB Function H


Subsidiary: Is there a way to add 2 counters ?

Code:
/home/me/myLibrary library_name_AAAAA Function A   001  00001
/home/me/myLibrary library_name_AAAAA Function B   002  00002
/home/me/myLibrary library_name_AAAAA Function C   003  00003
/home/me/myLibrary library_name_AAAAA Function D   004  00004
/home/me/myLibrary library_name_BBBBB Function E   001  00005
/home/me/myLibrary library_name_BBBBB Function F   002  00006
/home/me/myLibrary library_name_BBBBB Function G   003  00007
/home/me/myLibrary library_name_BBBBB Function H   004  00008


I am not looking for an algorithm to develop a program (python, java or other, although it is a good idea).

I am looking for a solution (bash script) based on the use of the sed, sort, uniq, awk, etc commands


With sed I know:

- select lines between two marker patterns
- capture group with '(' and ')'
- I heard about h, H, g, G commands but I'm never used


But I have no idea how to mix all.


Can you please help me ?
# 2  
Old 08-07-2018
Your sample data have DOS line terminators (<CR>, 0x0D, \r ^M) that need to be eliminated first. How about
Code:
awk '
                        {sub (/\r/, _)
                        }
/^ *Libray/             {LIB = $2
                        }
/^ *Source/             {SRC = $2
                        }
/^ *END/                {SA = 0
                        }
SA                      {CNT1++
                         CNT2++
                         printf "%s %s %s %03d %04d\n",  LIB, SRC, $0, CNT1, CNT2
                        }
/BEGIN SOURCE ANALYSIS/ {SA   = 1
                         CNT1 = 0
                        }
' file

This User Gave Thanks to RudiC For This Post:
# 3  
Old 08-08-2018
RudiC

It's really a very good solution.
That's exactly what I needed.
Thank you very much for this answer and for your responsiveness
# 4  
Old 08-08-2018
Is it really Libray?
Perhaps one should prepare for a bug fix with /^ *Librar?y/Smilie
# 5  
Old 08-08-2018
Well, I went for Library first, and changed only when failed...

Last edited by RudiC; 08-09-2018 at 05:26 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Text manipulation with sed/awk in a bash script

Guys, I have a variable in a script that I want to transform to into something else Im hoping you guys can help. It doesn't have to use sed/awk but I figured these would be the simplest. DATE=20160120 I'd like to transform $DATE into "01-20-16" and move it into a new variable called... (8 Replies)
Discussion started by: dendenyc
8 Replies

2. Shell Programming and Scripting

Text manipulation help

Hello Unix.com, How can i generate links like this: i got http://upload.com/1/1.txt and i need to generate links from http://upload.com/1/1.txt to http://upload.com/1000/1000.txt Thanks in advance, Galford D. Weller (4 Replies)
Discussion started by: galford
4 Replies

3. Shell Programming and Scripting

Text Manipulation Help

Hello unix.com people! How can I modify a text in format: A:B:C A:B:C A:B:C into C/A/B C/A/B C/A/B Note: Text is line by line and "C", "B", "A" fields are different each row. Thanks in advance. (7 Replies)
Discussion started by: galford
7 Replies

4. Shell Programming and Scripting

Advanced sed/awk help

I have thousands of files in HTML that looks like this: .... .... .... <!-- table horaire --> <!-- table horaire --> <table border="0" cellspacing="0" cellpadding="0" class="tblHoraires" summary="Table des horaires de la ligne 12"> <tr> <th scope="row"... (13 Replies)
Discussion started by: charafantah
13 Replies

5. Shell Programming and Scripting

sed usage with special characters - text manipulation

I'm trying to use sed to replace string in text file but I've some problems with slash and new-line for example I have to replace this string: \> signal_rssi=" or this string where new-line is in the middle of the string: " /> I'm using this code for the first case but it doesn't... (10 Replies)
Discussion started by: TheMrOrange
10 Replies

6. Shell Programming and Scripting

Advanced grep and sed

I am wondering if there is a way via grep and sed to extract a string that is on the 2nd line below a known marker as in this example: TextRel 203 0 0 "WELL:" SetPosAbs 1287 -6676 TextRel 210 0 0 "AEP #2" The marker is WELL:, but the string I need is "AEP #2". Can grep/sed handle this... (19 Replies)
Discussion started by: phudgens
19 Replies

7. UNIX for Dummies Questions & Answers

Help with text manipulation

Hi there, I have some text files in unix format that processed by a program in windows, and when I open them with less or vi in linux, a warn for opening binary file is prompted, and as shown in vi, between every two characters there was inserted a "^@". How can I fix this. Plus, there are over... (2 Replies)
Discussion started by: dustinwang2003
2 Replies

8. UNIX for Dummies Questions & Answers

text manipulation

I am tryin to figure out how to extract interested text from file example.txt blah blah blah a: child1 blah a: child2 blah b: parent1 blah blah blah .... blah a: child21 blah a: child22 blah a: child23 blah b: parent2 this kinda text repeats .. number of children is... (6 Replies)
Discussion started by: rajkishore
6 Replies

9. Shell Programming and Scripting

Advanced Search & Delete Text File

I have a file in which email messages are stored in. Every email is separated by by a ^Z character (Control-Z). I need to extract all emails after the 65,00th one to another file and delete them from the original file. Any suggests on accomplishing this? (2 Replies)
Discussion started by: maxcell
2 Replies

10. Shell Programming and Scripting

text manipulation

Hi, i have a file like this below, and it my have n no. of lines. Moderator gave me a solution with awk, but it was working only for the first 2 lines because awk has a limitation. can anyone give me the solution, thank you INPUT FILE: 1081 "WPCW 19 - CW/AM1, WPCB 40 - FAMN/CORNER, WPCB-DT1 50 -... (11 Replies)
Discussion started by: injeti
11 Replies
Login or Register to Ask a Question