LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-12-2019, 07:08 PM   #1
mechvijays
LQ Newbie
 
Registered: Mar 2019
Posts: 3

Rep: Reputation: Disabled
creating a sequence number for each block


I will rephrase my question :

input file :

AAAhjfhjhjd
BBdj
CCi
CCjid
AAAjdjido
BBdj
CCi
CCjid
dhdh

expected output :

1AAAhjfhjhjd
2BBdj
3CCi
4CCjid
1AAAjdjido
2BBdj
3CCi
4CCjid
5dhdh


whenever it see AAA at the start of the line it should reset the counter and again start form 1

Last edited by mechvijays; 03-13-2019 at 01:16 AM.
 
Old 03-12-2019, 09:51 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Welcome to LQ!

To preserve formatting place your code snippets inside [CODE]...[/CODE] tags. You may type those yourself or click the "#" button in the edit controls.

You appear to be using the right tool, awk, but it is not clear from your description how your data blocks are delineated. For example, does AAA****** always mark the beginning of a "block", or is the the END OF TRANSMISSION line?

If your code snippet represents your actual approach, then all you really need is a variable which is initially set to 1 for each file, then incremented at the beginning of each subsequent data block, wherever that may be found. Then simply append that variable to your print statement.

The way things usually work here at LQ is that you should try to find a working solution yourself, then ask for help when you get stuck somewhere, providing a minimal example which illustrates the difficulty you are having. This helps others to understand the problem itself and also gauge how to offer help in a way most appropriate to your demonstrated code preference and skill level.

You may want to review the Site FAQ for guidance in posting your questions and general forum usage. Especially, read the link in that page, How To Ask Questions The Smart Way. The more effort you put into understanding your problem and framing your questions, the better others can help!

Good luck!

Last edited by astrogeek; 03-12-2019 at 10:03 PM.
 
Old 03-12-2019, 11:55 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
What do you mean by block?
If you wanted file names and line-numbers prefixed to every line, you could simply do this:
Code:
fgrep -n '' *
Output:
Code:
proba1.txt:1:file1line1
proba1.txt:2:
proba2.txt:1:
proba2.txt:2:file2line2
 
1 members found this post helpful.
Old 03-13-2019, 01:16 AM   #4
mechvijays
LQ Newbie
 
Registered: Mar 2019
Posts: 3

Original Poster
Rep: Reputation: Disabled
I will rephrase my question :

input file :

AAAhjfhjhjd
BBdj
CCi
CCjid
AAAjdjido
BBdj
CCi
CCjid
dhdh

expected output :

1AAAhjfhjhjd
2BBdj
3CCi
4CCjid
1AAAjdjido
2BBdj
3CCi
4CCjid
5dhdh


whenver it see AAA at the start of the line it should reset the counter and again start form 1
 
Old 03-13-2019, 01:31 PM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Editing the original post then stating the same thing again is very confusing to those trying to undersstand your question. Please do not do that. Your restated question is also quite different than the original.

So, taking your restated question as the starting point, and continuing with awk as the tool of choice, that is a relatively simple problem. You have already answered it yourself, although you may not realize it.

You need only a counter variable which is incremented and prefixed to the print statement arguments, and reset to 1 each time if encounters AAA at the beginning of a line.

Take a look at your original awk code, ignore the filename portion of that code, and see if you can get it to work with a simple counter. If not, please post your code here so we can see what you have tried to do and try to explain how it is not working. That will help you to understand the problem better, and help those here to to provide useful replies.

Last edited by astrogeek; 03-13-2019 at 01:35 PM. Reason: typo
 
Old 03-13-2019, 02:16 PM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this InFile ...
Code:
AAAhjfhjhjd
BBdj
CCi
CCjid
AAAjdjido
BBdj
CCi
CCjid
dhdh
... this awk ...
Code:
awk '{if (index($0,"AAA")) c=1; print c++$0}' $InFile >$OutFile
... produced this OutFile ...
Code:
1AAAhjfhjhjd
2BBdj
3CCi
4CCjid
1AAAjdjido
2BBdj
3CCi
4CCjid
5dhdh
Daniel B. Martin

.
 
1 members found this post helpful.
Old 03-13-2019, 02:37 PM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this InFile ...
Code:
AAAhjfhjhjd
BBdj
CCi
CCjid
AAAjdjido
BBdj
CCi
CCjid
dhdh
... this awk ...
Code:
awk '{/^AAA/?c++:1; print c":"$0}' $InFile >$OutFile
... produced this OutFile ...
Code:
1:AAAhjfhjhjd
1:BBdj
1:CCi
1:CCjid
2:AAAjdjido
2:BBdj
2:CCi
2:CCjid
2:dhdh
This solution uses the ternary operator which is concise but less readable.

Daniel B. Martin

.

Last edited by danielbmartin; 03-13-2019 at 02:40 PM. Reason: Corrected missing colons
 
Old 03-13-2019, 02:37 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
hmmm .... would have liked to have seen OP have a go as this is not very complicated, but as a solution has been presented:
Code:
awk '/^AAA/{c=1}$0 = c++$0' file
 
2 members found this post helpful.
Old 03-13-2019, 02:39 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
Originally Posted by danielbmartin View Post
With this InFile ...
Code:
AAAhjfhjhjd
BBdj
CCi
CCjid
AAAjdjido
BBdj
CCi
CCjid
dhdh
... this awk ...
Code:
awk '{/^AAA/?c++:1; print c":"$0}' $InFile >$OutFile
... produced this OutFile ...
Code:
1AAAhjfhjhjd
2BBdj
3CCi
4CCjid
1AAAjdjido
2BBdj
3CCi
4CCjid
5dhdh
This solution uses the ternary operator which is concise but less readable.

Daniel B. Martin

.
Something missing in this one Daniel ... doesn't work for me:
Code:
$ awk '{/^AAA/?c++:1; print c":"$0}' f2
1:AAAhjfhjhjd
1:BBdj
1:CCi
1:CCjid
2:AAAjdjido
2:BBdj
2:CCi
2:CCjid
2:dhdh
 
1 members found this post helpful.
Old 03-13-2019, 09:16 PM   #10
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,791

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
The
Code:
awk '/^AAA/{c=1}$0 = c++$0' file
uses an implicit test and print. The test can have side effects. For example, an initial empty line or "0" line is not printed, because $0 becomes "0" or "00" and the implicit test sees a number and becomes false.
An explicit print makes things clear:
Code:
awk '/^AAA/{c=1} {print c++$0}' file
Initial lines are printed with prefixes 0,1,2 regardless of their contents.
I don't know what behavior the O/P intended but software should be predictable, especially if it controls a Boeing 737 max 8...
 
2 members found this post helpful.
Old 03-14-2019, 09:19 AM   #11
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by grail View Post
Something missing in this one Daniel ... doesn't work for me ...
You are right. I screwed up.

mechvijays (the Original Poster) posed a similar question here:
https://www.linuxquestions.org/quest...ne-4175650080/

That post asked for ...
Code:
1:AAAcjjjef
1:BBBifhfh
1:CCoiwfji
1:CCCifhifei
1:YYYjhcjh
1:ZZZjiddi
2:AAAcjjjeffdd
2:BBBifhdcdfh
2:CCoiwfdji
2:CCCifhdifei
2:YYYjhdcjh
2:ZZZjidddi
I wrote a solution for that question and mistakenly posted it on this thread.

Daniel B. Martin

.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Counting how many digits by each sequence in ip pedropt Programming 15 09-26-2018 04:40 PM
Identify and explain the major number, minor number, and revision number in Linux... turbomen Linux - Newbie 1 11-16-2010 02:48 AM
why sector number not match block number? bitzsk Linux - Kernel 1 06-09-2009 05:32 AM
How do you customize scripts' sequence number for different runlevels? Akhran Debian 2 03-25-2006 03:23 AM
udp + sequence number!!!!!! alaios Linux - Networking 1 07-27-2005 07:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration