Execution Problems with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Execution Problems with awk
# 1  
Old 01-13-2018
Execution Problems with awk

Ubuntu, Bash 4.3.48

Hi,

I have this input file:
Code:
a1:b2:c30:g4:h12:j7

and I want this output file:
Code:
a1=g4:b2=h12:c30=j7

I can do it this with this code:
Code:
awk -F':' '{print $1"="$4":"$2"="$5":"$3"="$6"}' INPUT > OUTPUT

In this case I have 6 columns, I calculate manually the half number of columns (6/2=3) and I wrotte $1 + $4 ... $3 + $6

Now I want to update my code with one that do everything in automatic, considering that the number of columns of the input file is unknown.

Some additional codes...

1) awk -F':' '{print NF; exit}' INPUT > COUNT_NF # with this I know the number of columns

2) I need a code to do this: NF/2= hNF (half of NF)

3) I think that for the main code I have to create a loop... like this

$1 "=" $(hNF+1) this to repeat for hNF times (i=hNF)

example...
Code:
awk -F':' -v hNF=$hNF ' { for (i=1; i<=hNF; i++) print $1"="$(hNF+1)":" }' INPUT > OUTPUT

Could you help me please!
Thanks

Last edited by Scrutinizer; 01-13-2018 at 11:08 AM.. Reason: Additional code and icode tags
# 2  
Old 01-13-2018
You can determine hNF in awk, just before the for loop.
# 3  
Old 01-13-2018
I don't know how... I'm not expert on this... I took the code from other sites :-/

Code:
awk -F':' '{print NF; exit}' INPUT | awk '{print $1 / 2}'  > halfcount

aaa=$(cat halfcount)

awk -F':' -v bbb=$aaa '{for (i=1; i<=aaa; i++) {print ($i)"="$(aaa+$i)":"} }' INPUT > OUTPUT

does not work :-/

Last edited by echo manolis; 01-13-2018 at 10:16 AM..
# 4  
Old 01-13-2018
Here are a couple of options you could try:
Code:
awk '{hNF=NF/2; s=""; for(i=1; i<=hNF; i++) s=s (i==1?"":OFS) $i "=" $(hNF+i); print s}' FS=: OFS=: file

Code:
awk '{hNF=NF/2; for(i=1; i<=hNF; i++) printf "%s=%s%s",$i,$(hNF+i),(i<hNF)?OFS:ORS}' FS=: OFS=: file

Code:
awk '{n=split($0,F); $0=""; for(i=1; i<=n/2; i++) $i=F[i] "=" F[i+n/2] }1' FS=: OFS=: file

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 01-14-2018
is perfect! thank you !!!
# 6  
Old 01-14-2018
Hello echo manolis,

Following sed solution may also help you in same in case your Input_file has 6 columns(mentioned by you in your post).
Code:
sed 's/\([^:]*\):\([^:]*\):\([^:]*\):\([^:]*\):\([^:]*\):\([^:]*\)/\1=\4:\2=\5:\3=\6/'  Input_file

Output will be as follows.
Code:
a1=g4:b2=h12:c30=j7

Thanks,
R. Singh
# 7  
Old 01-14-2018
Post solved
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Execution problems

How to find a word in a directory which contains many files? i just want to count how many such words are present in all the files? This is the code which i tried for a single file echo "Enter the file name:" read file echo "Enter the word to search:" read word if then echo "The count... (4 Replies)
Discussion started by: Meeran Rizvi
4 Replies

2. HP-UX

Execution problems with swreg

HP UX 10.20 I have a directory "/var/spool/sw" which is supposed to be a "Depot" directory. Turns out it is not and when using the swreg command "swreg -l /var/spool/sw" I get errors in part ERROR for option "-l /var/spool/sw" keyword or it's value may be incorrect or the keyword does not apply... (3 Replies)
Discussion started by: Randydog
3 Replies

3. UNIX for Dummies Questions & Answers

Execution Problems with Crontab

Dear Folks, I have written a shell script which internally connects to oracle database through sqplplus command line. The script runs fine when run manually. Now I am scheduling it to run (Linux environment) from crontab. In crontab it is failing with an error: sqlplus command... (6 Replies)
Discussion started by: tamojitc
6 Replies

4. UNIX for Dummies Questions & Answers

Execution Problems with UNIX.

Hi Team, I created one file like tst.pl, it contains #!/usr/bin/perl use Spreadsheet::ParseExcel; During execution it's showing error like use: command not found. Pleast let me suggest how to use this perl menthods in Unix. Thanks in Advance, Reards, Harris (5 Replies)
Discussion started by: harris
5 Replies

5. Shell Programming and Scripting

Execution Problems with if statements

Hi all, I habe a file called test.log, which contain following data : 0.0 0.1 0.1 0.1 0.1 0.2 0.3 0.3 0.4 0.4 0.6 8.7 8.8 17.2 I want to show the data which gater than 9.0 But my script not working. (4 Replies)
Discussion started by: mnmonu
4 Replies

6. Shell Programming and Scripting

Execution Problems

this my source file ************* fixed *************** Begin equipmentId : d9 processor : fox number : bhhhhhh Variable # 1: Id : 100 Type : 9 nType : s gType : 5f mType : 4 LField : England DataField : london Length ... (6 Replies)
Discussion started by: teefa
6 Replies

7. Shell Programming and Scripting

Execution Problems!!

i have been working on this for a about 12 hours today say's end of file un expected any idea's using the bourne shell and its driving me nuts worked fine in bash but prof says make it work in bourne and good luck worth 13% any help would be awesome #!/bin/sh trap "rm mnt2/source/tmp/* 2>... (1 Reply)
Discussion started by: mrhiab
1 Replies

8. UNIX and Linux Applications

Execution Problems with Cron

Hi all!! I have a nerve-wracking concept (probably for me!!) which is not understood. My crontab entry looks this way. 33 09 22 3 * /home/myexp.sh "Bgp4 ALL" >/dev/null 2>&1 But cron gets started occasionally. Sometimes it does. Sometimes it does not. And sometimes it hangs in the middle (I... (1 Reply)
Discussion started by: dhivyasuresh
1 Replies

9. Shell Programming and Scripting

Execution problems using awk command.

Hi All, I have the following requirement. In a directory i get files from external source. I at regular intervals check that directory for any incoming files. The file name is underscore delimited. Such as: aaa_bbb_ccc_ddd_eee_fff.dat I am using awk and and splitting the file name. ... (4 Replies)
Discussion started by: satishpv_2002
4 Replies

10. UNIX for Dummies Questions & Answers

Execution problems with crontab

I need your help please Inin a production system i found crontab entries was removed because i typed crontab -l with username corasc and didn't show anything. i asked the administrador to restored the mentioned crontab, he restored the crontab: The problem is when restored the crontab file is... (2 Replies)
Discussion started by: alexcol
2 Replies
Login or Register to Ask a Question