awk

How to remove leading and trailing whitespaces?

主宰稳场 提交于 2021-02-06 10:07:36
问题 I'm using awk '{gsub(/^[ \t]+|[ \t]+$/,""); print;}' in.txt > out.txt to remove both leading and trailing whitespaces. The problem is the output file actually has trailing whitespaces! All lines are of the same length - they are right padded with spaces. What am I missing? UPDATE 1 The problem is probably due to the the fact that the trailing spaces are nor "normal" spaces but \x20 characters (DC4). UPDATE 2 I used gsub (/'[[:cntrl:]]|[[:space:]]|\x20/,"") an it worked. Two strange things:

Awk - Count Each Unique Value and Match Values Between Two Files

我只是一个虾纸丫 提交于 2021-02-05 12:19:45
问题 I have two files. First I am trying to get the count of each unique field in column 4. And then match the unique field value from the 2nd column of the 2nd file. File1 - column 4's each unique value and File2 - columns 2 contains the value that I need to match between the two files So essentially, I am trying to -> take each unique value and value count from column 4 from File1, if there is a match in column2 of file2 File1 1 2 3 6 5 2 3 4 5 1 3 5 7 6 1 2 3 4 6 2 4 6 6 5 1 File2 hello "6" hi

Run awk comand on a remote server through ssh

血红的双手。 提交于 2021-02-05 11:46:45
问题 I'm trying to get a specific set of values from a file on a remote server. The command works fine when executing that through terminal. First ssh Command sshpass -p password ssh -T user@ip Second Awk Command find /opt/Info_Source/*daily* -type f -mtime -1 -exec zcat {} \; 2>/dev/null | awk -F, -v OFS=',' '$5 ~ /Valid/ && length($2) {print $2}' but if I combine both of them in a script #!/bin/ksh emp_id=`sshpass -p password ssh -T user@ip -q << EOF find /opt/Info_Source/*daily* -type f -mtime

AWK Compare two files and add new column from second file to first file if match

自闭症网瘾萝莉.ら 提交于 2021-02-05 11:30:27
问题 I have problem I would like to compare two files. First files is reference: ABCA4 INHR AMT INHR BTK ONKO1 PAP ONKO2 Second file is for compare: 3 1:2 T ENG1 ABCA4 ff 3 1:2 T ENG1 ABCA4 gg 5 1:4 A ENG20 AMT ll 6 1:5 G ENG12 BRB ds 7 1:6 T ENG8 PAP rg 7 1:6 T ENG8 PAP tt And I want compare $1 from first file with $5 from second file and if there is match print between $5 and $6 in second file the $2 from first file: 3 1:2 T ENG1 ABCA4 INHR ff 3 1:2 T ENG1 ABCA4 INHR gg 5 1:4 A ENG20 AMT INHR ll

adding && \ to each line on a text file except last line

点点圈 提交于 2021-02-05 09:11:46
问题 I'm trying to add && \ on end of each line on a text file except the last line. Sample input: ps mkdir repo cd repo/ touch file1.txt Expected output: ps && \ mkdir repo && \ cd repo/ && \ touch file1.txt First attempt I tried this, but it outputs && \ on each line including the final line : awk '{print $0"&& \\"}' RS="\r*\n\r*" Second attempt I tried using sed: sed '1s/^//;$!s/$/"&&" \\/;$s/$//' This seems to add extra newlines: ps && \ mkdir repo && \ cd repo/ && \ touch file1.txt 回答1: You

adding && \ to each line on a text file except last line

北城余情 提交于 2021-02-05 09:11:38
问题 I'm trying to add && \ on end of each line on a text file except the last line. Sample input: ps mkdir repo cd repo/ touch file1.txt Expected output: ps && \ mkdir repo && \ cd repo/ && \ touch file1.txt First attempt I tried this, but it outputs && \ on each line including the final line : awk '{print $0"&& \\"}' RS="\r*\n\r*" Second attempt I tried using sed: sed '1s/^//;$!s/$/"&&" \\/;$s/$//' This seems to add extra newlines: ps && \ mkdir repo && \ cd repo/ && \ touch file1.txt 回答1: You

adding && \ to each line on a text file except last line

余生颓废 提交于 2021-02-05 09:11:01
问题 I'm trying to add && \ on end of each line on a text file except the last line. Sample input: ps mkdir repo cd repo/ touch file1.txt Expected output: ps && \ mkdir repo && \ cd repo/ && \ touch file1.txt First attempt I tried this, but it outputs && \ on each line including the final line : awk '{print $0"&& \\"}' RS="\r*\n\r*" Second attempt I tried using sed: sed '1s/^//;$!s/$/"&&" \\/;$s/$//' This seems to add extra newlines: ps && \ mkdir repo && \ cd repo/ && \ touch file1.txt 回答1: You

adding && \ to each line on a text file except last line

倖福魔咒の 提交于 2021-02-05 09:07:52
问题 I'm trying to add && \ on end of each line on a text file except the last line. Sample input: ps mkdir repo cd repo/ touch file1.txt Expected output: ps && \ mkdir repo && \ cd repo/ && \ touch file1.txt First attempt I tried this, but it outputs && \ on each line including the final line : awk '{print $0"&& \\"}' RS="\r*\n\r*" Second attempt I tried using sed: sed '1s/^//;$!s/$/"&&" \\/;$s/$//' This seems to add extra newlines: ps && \ mkdir repo && \ cd repo/ && \ touch file1.txt 回答1: You

Trimming a part of file extension for all the files in directory - Linux

巧了我就是萌 提交于 2021-02-05 08:45:15
问题 I have a requirement in which I want to trim the file extension for all the files contained in a directory: - The file will be like; America.gz:2170 Europe.gz:2172 Africa.gz:2170 Asia.gz:2172 what I need is to trim the :2170 and :2172 from all the files, so that only the .gz extension remains. I know that with the help of below SED code, it is possible for all the entries in a file, however I need for all the files in a directory: - **sed 's/:.*//' file** Any bash or awk code to fix this will

Convert space separated file to tab separated file

Deadly 提交于 2021-02-05 08:41:32
问题 I was trying to use awk to convert a space delimited file into a tab delimited file. To my surprise this didn't work as expected: awk -vOFS=$'\t' '{print}' column.txt item gram tomato 500 orange 1500 bread 2000 I was expecting something like this: item gram tomato 500 orange 1500 bread 2000 What am I missing ? Note: many have suggested several alternatives, here are some of them maybe someone is interested. tr -s " " "\t" <column.txt column -t <column.txt perl -ple "s/\s+/\t/g" <column.txt