awk

How to increase value of a text variable in a file

瘦欲@ 提交于 2021-01-28 21:24:55
问题 file1.text contains below data. VARIABLE=00 RATE=14 PRICE=100 I need to increment value by 1 only for below whenever I want. VARIABLE=00 file name: file1.txt output should be incremented by 1 every time. output will be like below VARIABLE=01 in next run VARIABLE=02 and so on.... 回答1: Could you please try following, written and tested with shown samples in GNU awk . awk 'BEGIN{FS=OFS="="} /^VARIABLE/{$NF=sprintf("%02d",$NF+1)} 1' Input_file > temp && mv temp Input_file Explanation: Adding

Extract values from specific row, column using bash

拜拜、爱过 提交于 2021-01-28 15:14:58
问题 I have a sample log file with fields and values in this format value1 value2 50 100 value3 value4 10 150 value5 200 I need to extract fields and values to something of this format value1=50 value2=100 value3=10 value4=150 value5=200 回答1: Try this awk: gawk '{split($0,n_arr," "); getline; n=split($0,v_arr," "); getline; for (i=1;i<=n;i++){print n_arr[i] "=" v_arr[i]}}' 回答2: awk '/value/ { if ((getline values) > 0) { split(values, array) for (i = 1; i <= NF; i++) print $i "=" array[i] } }'

Extract values from specific row, column using bash

给你一囗甜甜゛ 提交于 2021-01-28 15:10:44
问题 I have a sample log file with fields and values in this format value1 value2 50 100 value3 value4 10 150 value5 200 I need to extract fields and values to something of this format value1=50 value2=100 value3=10 value4=150 value5=200 回答1: Try this awk: gawk '{split($0,n_arr," "); getline; n=split($0,v_arr," "); getline; for (i=1;i<=n;i++){print n_arr[i] "=" v_arr[i]}}' 回答2: awk '/value/ { if ((getline values) > 0) { split(values, array) for (i = 1; i <= NF; i++) print $i "=" array[i] } }'

Extract values from specific row, column using bash

社会主义新天地 提交于 2021-01-28 15:07:04
问题 I have a sample log file with fields and values in this format value1 value2 50 100 value3 value4 10 150 value5 200 I need to extract fields and values to something of this format value1=50 value2=100 value3=10 value4=150 value5=200 回答1: Try this awk: gawk '{split($0,n_arr," "); getline; n=split($0,v_arr," "); getline; for (i=1;i<=n;i++){print n_arr[i] "=" v_arr[i]}}' 回答2: awk '/value/ { if ((getline values) > 0) { split(values, array) for (i = 1; i <= NF; i++) print $i "=" array[i] } }'

Extract values from specific row, column using bash

安稳与你 提交于 2021-01-28 15:05:28
问题 I have a sample log file with fields and values in this format value1 value2 50 100 value3 value4 10 150 value5 200 I need to extract fields and values to something of this format value1=50 value2=100 value3=10 value4=150 value5=200 回答1: Try this awk: gawk '{split($0,n_arr," "); getline; n=split($0,v_arr," "); getline; for (i=1;i<=n;i++){print n_arr[i] "=" v_arr[i]}}' 回答2: awk '/value/ { if ((getline values) > 0) { split(values, array) for (i = 1; i <= NF; i++) print $i "=" array[i] } }'

Extract values from specific row, column using bash

ε祈祈猫儿з 提交于 2021-01-28 15:01:16
问题 I have a sample log file with fields and values in this format value1 value2 50 100 value3 value4 10 150 value5 200 I need to extract fields and values to something of this format value1=50 value2=100 value3=10 value4=150 value5=200 回答1: Try this awk: gawk '{split($0,n_arr," "); getline; n=split($0,v_arr," "); getline; for (i=1;i<=n;i++){print n_arr[i] "=" v_arr[i]}}' 回答2: awk '/value/ { if ((getline values) > 0) { split(values, array) for (i = 1; i <= NF; i++) print $i "=" array[i] } }'

Appending 0's to a file in unix/bash if the line is less than a fixed length

浪子不回头ぞ 提交于 2021-01-28 13:57:39
问题 I am looking for a way in which to append 0's to the end of multiple lines in a file if they are less than 66 characters in length so that the total line length equals 66. Here's an example of the file format (... denotes preceding 56 characters): $cat file1 ...1234567891 ...123456 ... ...12345678 Ideal format: ...1234567891 ...1234560000 ...0000000000 ...1234567800 回答1: GNU awk solution: $ awk '{s=$0; while(length(s)<66) s=s "0"; print s}' file1 or even shorter: $ awk '{while(length<66) $0=

Bash: replace entire string in one column based on matching substring

怎甘沉沦 提交于 2021-01-28 12:43:50
问题 I have a large file with many columns and rows. I would like to replace an entire string in the first column based on a substring that's common to all strings I want to replace. Here's an example of what I have: AAA_1765 866 HTG AAA_1873 987 IGA AAA_1922 413 BOK I would like all strings in the first column that contain the substring AAA_1 be entirely replaced with another string, so that it looks like this: BBB_2 866 HTG BBB_2 987 IGA BBB_2 413 BOK I've been working with sed to do a search

Remove duplicate line only contain specific string

只谈情不闲聊 提交于 2021-01-28 12:10:57
问题 I try to remove duplicates lines only if contain a specific string. It's easy to remove only duplicates lines, but some useful lines is deleted with : awk '!seen[$0]++' or perl -ne 'print unless $seen{$_}++' Exemple : keep first occurence of lines containing "host_name=" keep all occurrences of lines containing "plugin output=" with above awk or Perl commands that delete the client number too. My output command : host_name=Client1 plugin_output=Name : Client1 Marseille host_name=Client1

Awk column with pattern array

試著忘記壹切 提交于 2021-01-28 11:47:54
问题 Is it possible to do this but use an actual array of strings where it says "array" array=(cat dog mouse fish ...) awk -F "," '{ if ( $5!="array" ) { print $0; } }' file I would like to use spaces in some of the strings in my array. I would also like to be able to match partial matches, so "snow" in my array would match "snowman" It should be case sensitive. Example csv s,dog,34 3,cat,4 1,african elephant,gd A,African Elephant,33 H,snowman,8 8,indian elephant,3k 7,Fish,94 ... Example array