How to get the formatted string with reg?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get the formatted string with reg?
# 1  
Old 01-27-2011
How to get the formatted string with reg?

Source is
Code:
" LG     0 0   18   14   172.16.2.20        1      2    OK T    OK T    OK T"

How to get the following format string with reg?
Code:
LG 0 0, 18

Thanks in advance.

Last edited by Franklin52; 01-28-2011 at 03:34 AM.. Reason: Please use code tags
# 2  
Old 01-27-2011
Something like this,

Code:
echo '" LG 0 0 18 14 172.16.2.20 1 2 OK T OK T OK T"' | perl -nle 'if (/^"\s?(\w+\s+\d+\s+\d+)(\s+\d+)/) { print "$1,$2"}'

# 3  
Old 01-27-2011
Thanks pravin27,
Can it be implemented with sed?
# 4  
Old 01-27-2011
Could this help ?

Code:
echo '" LG 0 0 18 14 172.16.2.20 1 2 OK T OK T OK T"' | sed 's/" \(.. . .\)\( ..\).*/\1,\2/g'

OR
Code:
echo '" LG 0 0 18 14 172.16.2.20 1 2 OK T OK T OK T"' | sed 's/" \+\([A-Za-z]\+ \+[0-9]\+ \+[0-9]\+\)\( \+[0-9]\+\).*/\1,\2/g'


Last edited by pravin27; 01-27-2011 at 05:18 AM..
# 5  
Old 01-27-2011
Thanks Pravin27 again.
I tried the following sed, it strange that there is no change for the source. What's wrong with the expression?
Code:
sed 's/" \(\w+\s+\d+\s+\d+\)\(\s+\d+\).*/\1,\2/g'

---------- Post updated at 04:48 AM ---------- Previous update was at 03:45 AM ----------

source
Code:
" LG     0 0   18   14   172.16.2.20        1      2    OK T    OK T    OK T"

need to format to
Code:
"LG     0 0, 18"


when tried to use sed like this
Code:
sed 's/\(s+\)(\w+\s+\d+\s+\d+\)\(\s+\d+\).*/\2\,\3/' pmnode

occur the following error:
Code:
sed: command garbled: s/\(s+\)(\w+\s+\d+\s+\d+\)\(\s+\d+\).*/\2\,\3/

Can someone tell me what's wrong with command?

Thanks in advance.

Moderator's Comments:
Mod Comment Please use code tags when posting data and code samples!

Last edited by Franklin52; 01-28-2011 at 03:33 AM..
# 6  
Old 01-27-2011
Try like this,

Code:
sed 's/\(\w\+\s\+\w\+\s\+\w\+\)\(\s\+\w\+\).*/\1,\2/g'

# 7  
Old 01-27-2011
Hi pravin27, thank you very much.

I tried and it doesn't still work.

So far, perl -nle command can work.

I have a question, if the source is a file, how to make the format with perl -nle?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

How to prevent incorrect string using reg expr in Java?

Hi All, I need your input on how to mask out / ignore a string that does not match a working regular expression (continually refining) pattern in Java. Below is the code snippet which is picking up all the lines with the correct regular expression string except one known so far: public... (0 Replies)
Discussion started by: gjackson123
0 Replies

2. UNIX for Dummies Questions & Answers

Help creating well-formatted columns

Hi all I'm having a few issues with sorting some data into easily-readable columns. Original data in file: Number of visits IP Address 8 244.44.145.122 8 234.45.165.125 6 225.107.26.10 I firstly tried the column -t command which results in this: Number of ... (4 Replies)
Discussion started by: semaj
4 Replies

3. UNIX for Advanced & Expert Users

A formatted df script or alias ... ???

Hi gurus, Does anyone have a df script/alias that is sort of "universal"? Just getting frustrated to use bdf for HP-UX, df -h for Linux and Solaris, df -G/g (???) for AIX ... and to make things even worse, some are NFS mount points or with long logical volume name and it extends over two (2)... (1 Reply)
Discussion started by: newbie_01
1 Replies

4. Shell Programming and Scripting

String searching and output to a file in a formatted text

Hi, I'm very new to UNIX scripting and find quite difficult to understand simple UNIX syntax. Really appreciat if somebody could help me to give simple codes for my below problems:- 1) I need to search for a string "TTOH 8031950001" in a files which filename will be "*host*'. For example, the... (3 Replies)
Discussion started by: cuji
3 Replies

5. UNIX for Dummies Questions & Answers

Finding string and getting formatted ouput

I have follwoing text file as alarm dump. A1/EXT "B25I11K0150_F W" 512 090629 1121 RADIO X-CEIVER ADMINISTRATION BTS EXTERNAL FAULT MO RSITE CLASS RXOCF-405 DKRD01_INDS 1 EXTERNAL ALARM MAINS FAIL ... (1 Reply)
Discussion started by: lalchand
1 Replies

6. Shell Programming and Scripting

More formatted string

In a large file i need last line of the file but I want ignore first charcter, ignore leading zeros after first character and print the remaining character as such. Is something i can do it by tail or wc? cat test1 .... ...... 8000003687 cat test2 .... ...... 8000538990 o/p 3687 538990 (3 Replies)
Discussion started by: ford2020
3 Replies

7. Shell Programming and Scripting

reg String comparision

Hi, I would like to compare the 25th position of the file with the character '(' and if it is not equal then it would generate a mail. I have used the below if condition, however it is always executing the code within if, even the comparison is expected to return false. if Please help me... (1 Reply)
Discussion started by: kdheepan
1 Replies

8. Shell Programming and Scripting

Formatted Output

Hi I have the following lines in a file SWPRC000001NOT STATED 1344 SWPRC000001NOT STATED 1362 SWPRC000001NOT STATED 1418 SWPRC000001NOT STATED 1436 SWPRC000001NOT STATED ... (6 Replies)
Discussion started by: dhanamurthy
6 Replies

9. Shell Programming and Scripting

Formatted output - awk

Hi I have the following records in a file SABN YOURTUBE 000514 7256 SACN XYOUDSDF 000514 7356 SADN KEHLHRSER 000514 7656 SAEN YOURTUBE 000514 7156 SAFN YOURTUBE 000514 7056 I need to put this in the format like this printf '%s %-50s %6s %-6s\n' I am not going to read individual... (3 Replies)
Discussion started by: dhanamurthy
3 Replies

10. Shell Programming and Scripting

Convert DATE string to a formatted text

Hi guys, i need your help. I need to convert a date like this one 20071003071023 , to a formated date like 20071003 07:10:23 . Could this be possible ? Regards, Osramos (6 Replies)
Discussion started by: osramos
6 Replies
Login or Register to Ask a Question