awk concat lines between 2 sequent digits


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk concat lines between 2 sequent digits
# 1  
Old 10-25-2011
awk concat lines between 2 sequent digits

I would like to print string between two sequent digits and concatenate it into one single line.

input.txt

Code:
99 cord, rope, strand, twine,
twist, 100 strand, twine, twist,
cord, rope 101 strand, twine,
twist, twine, twist, cord, rope
105 cord, rope ,twi                                                 --> Here shall the script exit since it is larger than 102

output.txt

Code:
99 cord, rope, strand, twine, twist,
100 strand, twine, twist, cord, rope
101 strand, twine, twist, twine, twist, cord, rope

This is as far as i got
Code:
awk '/[0-9]/  {a=$0;c=1;next} /[0-9]/ {print a;a="";c=0;next}c==1{a=a"\n"$0} END{print a}'


Last edited by radoulov; 10-25-2011 at 06:46 PM.. Reason: Code tags!
# 2  
Old 10-25-2011
Are the commas really inconsistent, or is that a typo? (twist, 100 , but rope 101 )?

Also, do you want one single line in the output, or 3 as you show in your example?

Last edited by CarloM; 10-25-2011 at 10:22 AM..
# 3  
Old 10-25-2011
Quote:
Originally Posted by CarloM
Are the commas really inconsistent, or is that a typo? (twist, 100, but rope 101)?
Its a typo but the strings in between the two sequential digits is a mishmash of everything, even special characters and digits though not sequential.
# 4  
Old 10-25-2011
If you only want a single line in the output:
Code:
tr '\n\' \ \' < xx.txt |  
  awk -vVMIN=99 -vVMAX=102 '{ for (i=1;i<=NF;i++) { if (match ($i,"[0-9]+")) { if ($i > VMAX) { inside=0 }  else if ($i >= VMIN) { inside=1 } } if (inside==1) { printf ("%s ", $i) } } printf ("\n") }'

If you want a newline before each number:
Code:
tr '\n\' \ \' < xx.txt |  
  awk -vVMIN=99 -vVMAX=102 '{ for (i=1;i<=NF;i++) { if (match ($i,"[0-9]+")) { if ($i > VMAX) { inside=0 }  else if ($i >= VMIN) { inside=1 } } if (inside==1) {  if (match ($i,"[0-9]+")) {printf ("\n")} printf ("%s ", $i) } } printf ("\n") }'


Last edited by CarloM; 10-25-2011 at 11:08 AM..
This User Gave Thanks to CarloM For This Post:
# 5  
Old 10-25-2011
@ CarloM

Thanks for the code. It took me awhile to get the code digested. I came up with something like this, though it won't work the way I need it. On some lines it cuts of the rest after LF and then prints the next line of the following digit.

Code:
awk '/^[0-9]+/ {a=$0;c=1;next} /[0-9]+/ {print a;a="";c=0;next}c==1{a=a"\n"$0} END{print a}'

Can somebody fix it?
# 6  
Old 10-25-2011
Another awk version:
Code:
awk 'END {
  if (f) print f
  }
{
  for (i = 0; ++i <= NF;) {
   if (int($i) > max) break
   if ($i ~ /^[0-9]+$/ && min <= $i) {
      if (f) print f; f = x
      }
    f = f ? f FS $i : $i      
    }
  }' min=99 max=102 infile

This User Gave Thanks to radoulov For This Post:
# 7  
Old 10-25-2011
Code:
 
tr '\n' ' ' <input | awk '!f&&/[0-9]+/{if(!c++){r=$0;printf $0}else{if($0-r==1){r=$0;printf "\n" r}else{f=1}};next}!f{printf FS $0}END{print}' RS=" "

---------- Post updated at 12:22 AM ---------- Previous update was at 12:05 AM ----------

Lazy way :

Code:
 sed '2,$s/\([0-9][0-9]*\)/#\1/g;$s/.$/&#/' input | tr -s '\n#' ' \n' | head -3

This User Gave Thanks to ctsgnb For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed / awk script to delete the two digits from first 3 digits

Hi All , I am having an input file as stated below 5728 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r03_q_reg_20_/Q 011 611 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r04_q_reg_20_/Q 011 3486... (4 Replies)
Discussion started by: kshitij
4 Replies

2. Shell Programming and Scripting

Put in one line only the lines with digits

Hello, I haven't been here for a while and I might be forgetting some things. I have an input text like this titleA http://myurl/bla/blabla/1234 http://myurl/bla/blabla/6789 titleB http://myurl/bla/blabla/5678 http://myurl/bla/blabla/1234 titleC http://myurl/bla/blabla/9123... (10 Replies)
Discussion started by: Kibou
10 Replies

3. Shell Programming and Scripting

Concat String with variable after a 'grep' and awk

Here is the structure of my file: MyFile.txt g-4.n.g.fr 10.147.243.63 g-4.n.g.fr-w1 Here is my sript: test.sh #! /bin/sh ip=10.147.243.63 worker=$(grep -e $ip $1 | awk '{ print $3; }') echo "" echo $worker echo "" echo $worker echo "" echo "$worker.v.1" echo... (7 Replies)
Discussion started by: chercheur111
7 Replies

4. UNIX for Dummies Questions & Answers

concat any two lines in a file

I have a file with line 4 : F SITE SPA_M2 SPA_M3 SPA_M4 and a line 237 with: BV_N4 VbdGO_PW Rs_NW_STI Rc_N+OD need a awk liner to concat the two lines so that line 2 sits next to line1 and looks like: F SITE SPA_M2 SPA_M3 SPA_M4 BV_N4 VbdGO_PW ... (8 Replies)
Discussion started by: awkaddict
8 Replies

5. Shell Programming and Scripting

Need help in concat of two lines in a file

Hi , Need help in concating two lines based on certain character, for example my file has the messages : :57A:qweqweww :58A:qeqewqeqe -}$ {1:fffff2232323}{2:123123dasds}{4: :20:121323232323232 :21:sdsadasdasddadad if the line ends with "-}$" or if a line starts with "{1:" then it... (5 Replies)
Discussion started by: ulin
5 Replies

6. Shell Programming and Scripting

Awk Concat

Hi All this may be somewhere in internet , but couldnt find the it. i have file as abc01 2010-07-01 12:45:24 2010-07-01 12:54:35 abc02 2010-07-01 12:59:24 2010-07-01 01:05:13 abc03 . . . the output using awk should look like this abc01|2010-07-01 12:45:24|2010-07-01 12:54:35... (3 Replies)
Discussion started by: posner
3 Replies

7. Shell Programming and Scripting

How to concat lines that have the same key field

Hi, I have file like this - ABC 123 ABC 456 ABC 321 CDE 789 CDE 345 FGH 111 FGH 222 FGH 333 XYZ 678 I need the output like this: ABC 123,456 CDE 789,345 FGH 111, 222 XYZ 678 Meaning I want to concat the lines that have the same first column, but I only need the first two... (10 Replies)
Discussion started by: redwing
10 Replies

8. Shell Programming and Scripting

Conditional concat lines awk

Hello, I have a text file like this: NONE FILE_Rename frompath: /log_audit/AIX/log/current/AIXAFTPP.log NONE FILE_Unlink filename /audit/tempfile.14041142 NONE FILE_Rename ... (8 Replies)
Discussion started by: carloskl
8 Replies

9. Shell Programming and Scripting

concat 6 files in 1 file ( maybe use AWK?)

hi, I have the following problem: - 6 different files that have one key in common. - this six files must be aggregated in one output file sorted by the key. - the main file has to be writen twice, one in the beggining of the new output file and another in the end, for each key. - add one... (3 Replies)
Discussion started by: naoseionome
3 Replies

10. UNIX for Dummies Questions & Answers

mailx on sequent

Hi I am working on sequent unix. I would like to use the "mailx -c" (for the purpose of corbon copy). But I got error illigal option. I tried with tilda c (~c). but this works only from command line. Is there any way that I can use this from shellscript. I have looked in to our database... (1 Reply)
Discussion started by: reddyb
1 Replies
Login or Register to Ask a Question