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.
# 8  
Old 11-06-2009
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
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)

Previous Thread | Next Thread
Test Your Knowledge in Computers #722
Difficulty: Medium
Konrad Zuse built the first digital freely programmable computer, the Z1.
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