Put in one line only the lines with digits


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Put in one line only the lines with digits
# 1  
Old 09-06-2016
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

Code:
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
http://myurl/bla/blabla/1234
http://myurl/bla/blabla/8912

I need to extract only the title and then the digits at the end only, separated by comma.

Desired output:

Code:
titleA
1234,6789

titleB
5678,1234

titleC
9123,1234,8912

I am very unhappy at the moment because after trying I only got:

awk -F"/" '$1!~/^http/{print};{print $6}' cases.txt | awk '{ORS=","};{print}'
But this outputs everything separated by comma in the same line which is a mess.

Without the last formatting part, I got this in clear:
Code:
titleA
1234
6789

titleB
5678
1234

titleC
9123
1234
8912

But also full of many blank lines in between.

I really don't know how to move forward. Any hint will be appreciated. Thank you in advance.

I am working in an Ubuntu Linux right now.
# 2  
Old 09-06-2016
Hello Kibou,

Could you please try following.
Code:
awk '($0 ~ /title/){if(Q){print Q ORS $0;Q=""} else {print};next} {sub(/.*\//,X,$0);Q=Q?Q ($0?OFS $0:RS):$0;} END{print Q}' OFS=,   Input_file

Output will be as follows.
Code:
titleA
1234,6789
 
titleB
5678,1234

titleC
9123,1234,8912

EDIT: Adding above solution's little polish version.
Code:
awk '($0 ~ /title/){W=Q?Q ORS $0:$0;print W;W=Q="";next} {sub(/.*\//,X,$0);Q=Q?Q ($0?OFS $0:RS):$0} END{print Q}' OFS=,   Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 09-06-2016 at 12:59 PM.. Reason: Added one more solution now successfully.
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 09-06-2016
Code:
awk -F'/' '
        !/^http/ && NF {
                T = $0
                next
        }
        /^http/ {
                A[T] = ( T in A ? A[T] OFS $NF : $NF )
        }
        END {
                for ( k in A )
                        printf "%s\n%s\n\n", k, A[k]
        }
' OFS=, file

This User Gave Thanks to Yoda For This Post:
# 4  
Old 09-06-2016
Another approach:
Code:
awk '{for(i=2; i<=NF; i++) sub(".*/",x,$i); sub(OFS,FS)}1' FS='\n' OFS=, RS= ORS='\n\n' file



--edit--
Yet another option:
Code:
awk '{gsub("\n[^\n]*/",","); sub(",","\n")}1' RS= ORS='\n\n' file


Last edited by Scrutinizer; 09-06-2016 at 05:22 PM..
These 4 Users Gave Thanks to Scrutinizer For This Post:
# 5  
Old 09-06-2016
I really appreciate it. I am always speechless with your amazing approaches. Smilie
# 6  
Old 09-06-2016
Try also
Code:
awk '{printf "%s%s", (NF>1 && ONF>1)?",":LF, $NF; LF = RS; ONF = NF} END {printf RS}' FS=/ file
titleA
1234,6789

titleB
5678,1234

titleC
9123,1234,8912

It would work also if the empty lines were missing.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 09-07-2016
Code:
$ awk -F\/ '/^title/{print a;print;a="";}/^http/{a=a","$NF}END{print a}' a.txt | sed '1d;s/^,//'
titleA
1234,6789
titleB
5678,1234
titleC
9123,1234,8912

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

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Eliminating duplicate lines via specified number of digits

Hello, This is similar to a previous post, where I was trying to eliminate lines where column #1 is duplicated. If it is a duplicate, the line with the greater value in column #2 should be deleted. In this new case, I need to test duplication with the first three digits in column #1 (ignoring the... (6 Replies)
Discussion started by: palex
6 Replies

2. UNIX for Dummies Questions & Answers

Grep lines with numbers greater than 2 digits at the end of the line

I'm trying to grep lines where the digits at the end of each line are greater than digits. Tried this but it will only allow me to specify 2 digits. Any ideas would greatly be appreciated. grep -i '\<\{3,4,5\}\>' file ---------- Post updated at 05:58 PM ---------- Previous update was at 05:41... (1 Reply)
Discussion started by: jimmyf
1 Replies

3. Shell Programming and Scripting

Please Help! Need to put the lines of a txt to one line

Hi all, I'm quite newbie in shell scripting but I found a problem what I cant solve. I have a .txt file which looks like this: /22/ /23/ /24/ and so on and I'd need to make it look like this: /22/|/23/|/24/|...and so on. these numbers are growing and has lines like this /2a/ as well.... (15 Replies)
Discussion started by: gergo235
15 Replies

4. Shell Programming and Scripting

Find filenames with three digits and add zeros to make five digits

Hello all! I've looked all over the internet and this site and have come up a loss with an easy way to make a bash script to do what I want to do. I have a file with a naming convention as follows: 2012-01-18 string of words here 123.jpg 2012-01-18 string of words here 1234.jpg 2012-01-18... (2 Replies)
Discussion started by: Buzzman25
2 Replies

5. Shell Programming and Scripting

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 99 cord, rope, strand, twine, twist, 100 strand, twine, twist, cord, rope 101 strand, twine, twist, twine, twist, cord, rope 105 cord, rope ,twi ... (8 Replies)
Discussion started by: sdf
8 Replies

6. Shell Programming and Scripting

Remove line based on string and put new line with parameter

Hi Folks, I am new to ksh, i have informatica parameter file that i need to update everyday with shell script. i need your help updating this file with new parameters. sample data $$TABLE1_DATE=04-27-2011 $$TABLE2_DATE=04-23-2011 $$TABLE3_DATE=03-19-2011 .......Highligned... (4 Replies)
Discussion started by: victor369
4 Replies

7. Shell Programming and Scripting

Perl XML, find matching condition and grep lines and put the lines somewhere else

Hi, my xml files looks something like this <Instance Name="New York"> <Description></Description> <Instance Name="A"> <Description></Description> <PropertyValue Key="false" Name="Building A" /> </Instance> <Instance Name="B"> ... (4 Replies)
Discussion started by: tententen
4 Replies

8. Shell Programming and Scripting

using tr to put multiple lines of output into one line

Hi all, For a intro UNIX course I'm taking, I need to use the command "tr" to display a file on standard output without any newlines (all on one line). I assume I would start with "cat filename | tr" but don't know what to put after tr. Any ideas would be lovely! Thanks. (3 Replies)
Discussion started by: otes4
3 Replies

9. UNIX for Dummies Questions & Answers

Grep a line with between 3 and 5 digits

Hi, I am having problems using grep to extract only 3,4 or 5 digit numbers from a text file, using: grep '\<\{3,5\}\>' test.txt or grep '\{3,5\}' test.txt or egrep '{3,5}' test.txt I would appreciate any help that anyone can give me thanks (1 Reply)
Discussion started by: ceemh3
1 Replies
Login or Register to Ask a Question