Visit Our UNIX and Linux User Community


Awk, with separator |


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk, with separator |
# 1  
Old 11-25-2015
Awk, with separator |

Friends have the following code that is correct.

Code:
BEGIN { num_reg = 0
        suma_iva=0        
}
{  
        num_reg++
        suma_iva=suma_iva+int(substr($0, 103,9))
 }
END{
         printf ("%011d",suma_iva)
}

I have the following problem, I have to do just that but this time is not fixed position (substr ($ 0, 103.9)) is now by a separator "|" as I can do ???

example:

Code:
manuel|141568|chile|201|glosa


Last edited by Scrutinizer; 11-25-2015 at 04:09 PM..
# 2  
Old 11-25-2015
I presume you are talking of an awk script.

In awk, you can define the field separator, in this case "|", and count the number of fields. Unfortunately your data sample doesn't match script, but in the sample print substr ($0, 21, 3) would be equivalent to print $4.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 11-25-2015
Quote:
Originally Posted by RudiC
I presume you are talking of an awk script.

In awk, you can define the field separator, in this case "|", and count the number of fields. Unfortunately your data sample doesn't match script, but in the sample print substr ($0, 21, 3) would be equivalent to print $4.
riend what happens is the following:

input file:

Code:
manuel|50|glosa1
car|30|glosa2
mariajuana|40|glosa3

with the above code should I add the numeric variable column and save the value in this case is 120

---------- Post updated at 03:01 PM ---------- Previous update was at 03:00 PM ----------

passes that code is oriented when it is fixed but in this case would be separated by |
# 4  
Old 11-25-2015
suma_iva+=int($2)
This User Gave Thanks to RudiC For This Post:
# 5  
Old 11-25-2015
Quote:
Originally Posted by RudiC
suma_iva+=int($2)
It was perfect !!! Thank you very much for your help

I leave the code if you're interested

Code:
cat $PATH_DAT/mr.txt | nawk -v ciclo=$var 'BEGIN  { COUNT = 0;
                suma_neto=0
                FECHA="`date +%Y%m%d`"
                FS="|"
              }
              {
                num_reg++
                suma_neto=suma_neto+int($2)
                printf ("salida:",$suma_neto)
              }
END{
        printf ("%011d",suma_neto)
}
' > cambiaporta.sh


Previous Thread | Next Thread
Test Your Knowledge in Computers #848
Difficulty: Medium
The IEEE standardized Bluetooth as IEEE 802.15.1, but IEEE no longer maintains the Bluetooth standard.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk field separator not working

Hi, can some some help to get me the right results, I have few text files, need to grep few columns from each file and get the results in one row with comma separated. my code is #folder=/nz/kit/log/backupsvr folder=/export/home/nz/valai/tmpfiles/ echo $folder for entry in `ls... (4 Replies)
Discussion started by: ValaiG
4 Replies

2. Shell Programming and Scripting

Use string as Record separator in awk

Hello to all, Please some help on this. I have the file in format as below. How can I set the record separator as the string below in red "No. Time Source Destination Protocol Length Info" I've tried code below but it doesn't seem to... (6 Replies)
Discussion started by: cgkmal
6 Replies

3. Shell Programming and Scripting

How to use variable as separator in awk?

can some give me a example ? eg: a=usb I want to use variable "a" as separator (2 Replies)
Discussion started by: yanglei_fage
2 Replies

4. Shell Programming and Scripting

Field Separator in printf (awk)

I can not figure out how to set the Output filed separator in awk when using printf. Example: cat file some data here_is_more information Requested output some------------data her_is_more-----information Here are some that does not work: awk '{printf "%-15s %s\n",$1,$2}' OFS="-" file... (9 Replies)
Discussion started by: Jotne
9 Replies

5. Shell Programming and Scripting

awk field separator help -

Hi Experts , file : - How to construct the awk filed separator so that $1, $2 $3 , can be assigned to the each "" range. I am trying : awk -F"]" '{print $1}' but it is printing the entire file. Not first field. The desired output needed for first field... (9 Replies)
Discussion started by: rveri
9 Replies

6. Shell Programming and Scripting

awk field separator

I need to set awk field separator to ";", but I need to avoid ";EXT". so that echo a;b;c;EXTd;e;f | awk -F";" '{print $3}' would give "c;EXTd" (2 Replies)
Discussion started by: locoroco
2 Replies

7. UNIX for Dummies Questions & Answers

awk - output field separator

In awk, how do I print all fields with a specified output field separator? I have tried the following, which does not print the output FS: echo a b c d | awk 'BEGIN{OFS = ";"}{print $0}' (3 Replies)
Discussion started by: locoroco
3 Replies

8. Shell Programming and Scripting

awk (nawk) field separator

Hi; i have a file and i want to get; - If the last word in line 14 is NOT equal to "Set."; then print 2nd, 3rd, 4th and 5th values of 3rd line. and my code is: nawk 'NR==14 {if ($NF!="Set.") (NR==3{print $2,$3,$4,$5}) }' file.txt but no result?? :confused::(:confused::( (4 Replies)
Discussion started by: gc_sw
4 Replies

9. Shell Programming and Scripting

Field separator in awk

Hi I need to check if field separator I am using in awk statement is " : ", for example: TIME=12:59 HOUR=`echo "$TIME" | awk '{FS=":"; print $1}'` MINUTES=`echo "$TIME" | awk '{FS=":"; print $2}'` Is there a way to check within the above awk statement ? Thanks for help -A (2 Replies)
Discussion started by: aoussenko
2 Replies

10. Shell Programming and Scripting

awk spliting using separator

Hi I have a file which looks like this #HEllo #How.... #version 1.0.1 #Author aaaaa ab.-.1.-.90.-.80.-..-.OK cd.-.8.-.91.-.800.-.xy.-..-. the separator is .-. (dot hyphen dot) I want to display this as columns like ab cd 1 8 90 91 (1 Reply)
Discussion started by: PrasannaKS
1 Replies

Featured Tech Videos