The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Row to Columns question bleach8578 Shell Programming and Scripting 6 04-01-2009 08:39 AM
How to remove columns with specific string? AMBER UNIX for Dummies Questions & Answers 2 03-31-2009 03:18 PM
Perl, printing a string into columns doubleminus UNIX for Dummies Questions & Answers 5 05-21-2008 10:41 PM
Append string to columns from 2 files karthikn7974 Shell Programming and Scripting 3 04-28-2008 10:32 AM
Need help in AWK;Search String and rearrange columns spring_buck Shell Programming and Scripting 2 04-05-2007 12:40 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 3 Weeks Ago
liuzhencc liuzhencc is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 11
Curious question? How to put a string into two columns.

Now I have a list of numbers in hand and I try to put the numbers into two columns. Can I do this work with any script? Great thanks to your help!

Code:
1A1.log
HF=-240.451527
HF=-240.5213996
1A2.log
HF=-240.451527
HF=-240.5213996
1B.log
HF=-240.4273718
HF=-240.4956636
1C.log
HF=-240.4515273
HF=-240.5214005
1D1.log
1D2.log
1D.log
HF=-319.0829339
HF=-319.177099
I would like to get this output
Code:
1A1.log
HF=-240.451527   HF=-240.5213996
1A2.log
HF=-240.451527   HF=-240.5213996
1B.log
HF=-240.4273718   HF=-240.4956636
1C.log
HF=-240.4515273   HF=-240.5214005
1D1.log
1D2.log
1D.log
HF=-319.0829339   HF=-319.177099
It's better to get this
Code:
-240.451527     -240.5213996
-240.451527     -240.5213996
-240.4273718   -240.4956636
-240.4515273   -240.5214005
                                         !...two empty lines should be here...
                                         !...because of no numbers following "1D1.log" "1D2.log" in the input file, we should use two empty lines instead of them.
-319.0829339   -319.177099
Is this a stupid idea? Please help! thanks

Last edited by liuzhencc; 3 Weeks Ago at 07:51 AM..
  #2 (permalink)  
Old 3 Weeks Ago
scottn scottn is offline Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,053
Code:
awk '
  /^HF/ { printf $1 " "; getline }
  1
' file1

1A1.log
HF=-240.451527HF=-240.5213996
1A2.log
HF=-240.451527HF=-240.5213996
1B.log
HF=-240.4273718HF=-240.4956636
1C.log
HF=-240.4515273HF=-240.5214005
1D1.log
1D2.log
1D.log
HF=-319.0829339HF=-319.177099



awk -F= '
  /^HF/ { printf $2 " "; getline; print $2 }
' file1

-240.451527 -240.5213996
-240.451527 -240.5213996
-240.4273718 -240.4956636
-240.4515273 -240.5214005
-319.0829339 -319.177099

Last edited by scottn; 3 Weeks Ago at 07:39 AM..
  #3 (permalink)  
Old 3 Weeks Ago
steadyonabix steadyonabix is offline
Registered User
  
 

Join Date: Oct 2009
Location: UK
Posts: 174
Does each log only ever contain 2 lines?
  #4 (permalink)  
Old 3 Weeks Ago
liuzhencc liuzhencc is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 11
Quote:
Originally Posted by steadyonabix View Post
Does each log only ever contain 2 lines?
Not really. some did, but some logs have nothing follow it. Is this question clear? I just want to put the two numbers which behind the log in the same row.

---------- Post updated at 07:48 PM ---------- Previous update was at 07:42 PM ----------

Excellent script! It works well. But could you please help me to improve a little bit? If there are no numbers following the *log, we should put an empty line in the output. See the modified questing. Thank you very much! ....zhen
  #5 (permalink)  
Old 3 Weeks Ago
scottn scottn is offline Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,053
Code:
awk -F= '
  /.log/ && P { print "" }
  /.log/ { P=1 }
  /^HF/ { printf $2 " "; getline; print $2; P=0 }
' file1

-240.451527 -240.5213996
-240.451527 -240.5213996
-240.4273718 -240.4956636
-240.4515273 -240.5214005


-319.0829339 -319.177099
  #6 (permalink)  
Old 3 Weeks Ago
liuzhencc liuzhencc is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 11
Quote:
Originally Posted by scottn View Post
Code:
awk -F= '
  /.log/ && P { print "" }
  /.log/ { P=1 }
  /^HF/ { printf $2 " "; getline; print $2; P=0 }
' file1

-240.451527 -240.5213996
-240.451527 -240.5213996
-240.4273718 -240.4956636
-240.4515273 -240.5214005


-319.0829339 -319.177099
Perfect! It works. But I was so confused with this powerful script.
How did you put these empty lines?
Could you explain me something about this script.
I really want to learn something from here.
Thank you very much!

Very strange thing took place! see the following lines
Code:
1c1
< awk -F= '/.log/ && P { print "" }/.log/ { P=1 }/^HF/ { printf $2 "           " ; getline; print $2; P=0 }' TE.txt >> test7
---
> awk -F= '/.log/ && P { print "" }/.log/ { P=1 }/^HF/ { printf $2 "           " ; getline; print $2; p=0 }' TE.txt >> test7
I thought they are exactly the same code. But the outputs are totally different. The first line gave the correct output but the second line didn't.
Why? It's so strange that I cannot understand.
Output with the code of the first line
Code:
-240.451527           -240.5213996
-240.451527           -240.5213996
-240.4273718           -240.4956636
-240.4515273           -240.5214005


-319.0829339           -319.177099
-319.082436           -319.1777128
-319.0857963           -319.1786906
-319.0814125           -319.174904

-319.0814044           -319.1748736
-319.0827201           -319.173826
-319.0863378           -319.1788978
-319.075422           -319.1668696
-319.0650245           -319.1607778
-319.103556           -319.1942108
Output with the second line of the code.
Code:
-240.451527           -240.5213996

-240.451527           -240.5213996

-240.4273718           -240.4956636

-240.4515273           -240.5214005



-319.0829339           -319.177099

-319.082436           -319.1777128

-319.0857963           -319.1786906

-319.0814125           -319.174904


-319.0814044           -319.1748736

-319.0827201           -319.173826

-319.0863378           -319.1788978

-319.075422           -319.1668696

-319.0650245           -319.1607778

-319.103556           -319.1942108


-319.103556           -319.1942133

Last edited by liuzhencc; 3 Weeks Ago at 09:09 AM..
  #7 (permalink)  
Old 3 Weeks Ago
scottn scottn is offline Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,053
Quote:
How did you put these empty lines?
I took the lazy option of modifying the original script
Code:

 /.log/ && P { print "" }    # if the last line had ".log" in it (P > 0) print a blank line
 /.log/ { P=1 } # indicate we have a line with ".log" in it, the above line will evaluate to true if the next line has a ".log" in it
Code:

/^HF/ { printf $2 " "; getline; print $2; P=0 }
If a line starts with HF get the next line and print both together, reset P to say this line doesn't have ".log" in it. The HF= part is removed by using = as the field separator (-F=)

Or something like that!

I wouldn't really describe it as powerful.

---------- Post updated at 02:28 PM ---------- Previous update was at 01:47 PM ----------

Quote:
Originally Posted by liuzhencc View Post
Very strange thing took place! see the following lines
Code:
1c1
< awk -F= '/.log/ && P { print "" }/.log/ { P=1 }/^HF/ { printf $2 "           " ; getline; print $2; P=0 }' TE.txt >> test7
---
> awk -F= '/.log/ && P { print "" }/.log/ { P=1 }/^HF/ { printf $2 "           " ; getline; print $2; p=0 }' TE.txt >> test7
I thought they are exactly the same code. But the outputs are totally different. The first line gave the correct output but the second line didn't.
They're not the same. The second one has a lowercase p in the /^HF/ action. Variable names are case-sensitive in UNIX (almost universally so).

I should have used a less ambiguous variable name than P (which does look like p)!

(instead of editing your previous posts, it helps if you reply to the thread. that way I can see your updates more easily - I'm notified that way)
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:29 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0