Visit Our UNIX and Linux User Community


Curious question? How to put a string into two columns.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Curious question? How to put a string into two columns.
# 1  
Old 11-06-2009
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; 11-06-2009 at 07:51 AM..
# 2  
Old 11-06-2009
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 Scott; 11-06-2009 at 07:39 AM..
# 3  
Old 11-06-2009
Does each log only ever contain 2 lines?
# 4  
Old 11-06-2009
Quote:
Originally Posted by steadyonabix
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  
Old 11-06-2009
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  
Old 11-06-2009
Code:
nawk -F= ' (NF > 0){a = $2;getline;printf("%s\n", a" "$2);next}1' $(ls *.log)

# 7  
Old 11-06-2009
Quote:
Originally Posted by scottn
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; 11-06-2009 at 09:09 AM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #344
Difficulty: Medium
In February 2005, Sun announced the Sun Grid, a grid computing deployment on which it offered utility computing services priced at US$2 per CPU/hour for processing and per GB/month for storage.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printf or any other method to put long string of spec characters - passing passwords

Hello, I am looking for a method to use in my bash script which allows me to use long strings with all special characters. I have found that printf method could be helpful for me but unfortunately, when I trying root@machine:~# tevar=`printf "%s%c"... (2 Replies)
Discussion started by: elxa1
2 Replies

2. Shell Programming and Scripting

Put a string to the beginning of a file without a linefeed

Hello, I need to put the following string to the beginning of a file - but it should not create a newline at the end of the string. So, the file I have is a compressed one - with gzip. And I would like to add an ASCII-String to the beginning of the file. The string has a length of 69... (5 Replies)
Discussion started by: API
5 Replies

3. Programming

How to put dot(.) in a string in C?

Hi all, Can anybody please tell me how can I put dot(.) in a string using C programming. for example -- I am having string "10111988" and I want to convert it as "10.11.1988" I will appriciate and thanks in advance.. (4 Replies)
Discussion started by: smartgupta
4 Replies

4. Red Hat

Dummy Question, put a script on startup fails

Hi all, im pretty new to unit/redhat 6.2 I am trying to load a script upon startup of my redhat 6.2 (instance in ec2 AWS if someone cares) I want everytime when instance starts (after stop/reboot) a script i build to launch. when i run the script ./scriptname the script runs and everything is... (4 Replies)
Discussion started by: hookedatwalla
4 Replies

5. Shell Programming and Scripting

How to use sed to put string end of line?

I have several file as below, and i want to put .txt to specific text contain ^main=EXE^cmd=run script /usr/prog/bd_, file1 7.9102 12.1528 16.3672 7.4002 ^main=EXE^cmd=run script /usr/prog/bd_123^" line 16.3672 7.3134 17.8711 6.0981 file 2 7.9102 12.1528 16.3672 7.4002 ... (8 Replies)
Discussion started by: zulabc
8 Replies

6. Shell Programming and Scripting

put string end of the line

I've a problem to put .h end of the line..below my input file fg_a bb fg_b bb fg_c bb fg_d aa fg_f ee and i want the output file as below fg_a.h bb fg_b.h bb fg_c.h bb fg_d.h (6 Replies)
Discussion started by: zulabc
6 Replies

7. Shell Programming and Scripting

Extract columns from a file if the name dont exist put blank

Hi, I am very new to Unix script. Suppose i have a file with column header: NAME1 NAME2 Address Tel And I always need to make a file with column header: ID NAME1 NAME2 EMail Address Tel For the columns that do not exist in the file, I would still like to make a column with blank. ... (11 Replies)
Discussion started by: nightrider
11 Replies

8. Shell Programming and Scripting

How to compare particular string, if it is equal put into a new file

Hi, I have a file (sample.txt) this file contains below text. ./au ./11.5.0 ./11.5.0/admin ./11.5.0/admin/driver ./po ./11.5.0 ./11.5.0/admin ./11.5.0/admin/driver ./xxsbx/11.5.0/java/src ./xxsbx/11.5.0/lib ./xxsel ./xxsel/11.5.0 ./xxsel/11.5.0/admin ./zfa ./zfa/11.5.0... (2 Replies)
Discussion started by: gagan4599
2 Replies

9. Shell Programming and Scripting

Curious cron job question

Wow that was annoying, I wrote a huge long entire post out, clicked submit and the damn thing lost my entire post. Ok, here's what I am needing to do. Have a cron job run every six days at a random minute and hour ONLY ONCE on that day. Would this work? */30 */3 * * */6 command ... (3 Replies)
Discussion started by: Ashberry
3 Replies

10. Shell Programming and Scripting

put a string before a searched string

hi all! i have a working that looks like this... file1 MALE JOHN MALE ANJO FEMALE ANNE MALE JAMES FEMALE HONEY FEMALE IZA what i want to do is insert "M" when the first string is "MALE" and insert "F" when the first string is "FEMALE". file1 M MALE ... (10 Replies)
Discussion started by: kingpeejay
10 Replies

Featured Tech Videos