awk

Replacing string in linux using sed/awk based

余生颓废 提交于 2021-02-04 18:16:09
问题 i want to replace this #!/usr/bin/env bash with this #!/bin/bash i have tried two approaches Approach 1 original_str="#!/usr/bin/env bash" replace_str="#!/bin/bash" sed s~${original_str}~${replace_str}~ filename Approach 2 line=`grep -n "/usr/bin" filename` awk NR==${line} {sub("#!/usr/bin/env bash"," #!/bin/bash")} But both of them are not working. 回答1: You cannot use ! inside a double quotes in BASH otherwise history expansion will take place. You can just do: original_str='/usr/bin/env

Remove all text from last dot in bash

北战南征 提交于 2021-02-04 15:31:13
问题 I have a file named test.txt which has: abc.cde.ccd.eed.12345.5678.txt abcd.cdde.ccdd.eaed.12346.5688.txt aabc.cade.cacd.eaed.13345.5078.txt abzc.cdae.ccda.eaed.29345.1678.txt abac.cdae.cacd.eead.18145.2678.txt aabc.cdve.cncd.ened.19945.2345.txt If I want to remove everything beyond the first . like: cde.ccd.eed.12345.5678.txt cdde.ccdd.eaed.12346.5688.txt cade.cacd.eaed.13345.5078.txt cdae.ccda.eaed.29345.1678.txt cdae.cacd.eead.18145.2678.txt cdve.cncd.ened.19945.2345.txt Then I will do for

Remove all text from last dot in bash

本小妞迷上赌 提交于 2021-02-04 15:30:41
问题 I have a file named test.txt which has: abc.cde.ccd.eed.12345.5678.txt abcd.cdde.ccdd.eaed.12346.5688.txt aabc.cade.cacd.eaed.13345.5078.txt abzc.cdae.ccda.eaed.29345.1678.txt abac.cdae.cacd.eead.18145.2678.txt aabc.cdve.cncd.ened.19945.2345.txt If I want to remove everything beyond the first . like: cde.ccd.eed.12345.5678.txt cdde.ccdd.eaed.12346.5688.txt cade.cacd.eaed.13345.5078.txt cdae.ccda.eaed.29345.1678.txt cdae.cacd.eead.18145.2678.txt cdve.cncd.ened.19945.2345.txt Then I will do for

unix sort for 2 fields numeric order

心不动则不痛 提交于 2021-02-04 13:56:26
问题 I need to sort some data with unix sort but I can't figure exactly the right syntax, the data looks like 3.9.1 Step 10: 3.9.1 Step 20: 3.8.10 Step 20: 3.10.2 Step 10: 3.8.4 Step 90: 3.8.4 Step 100: 3.8.4 Step 10: I want to sort it using first the major number, then the step number, e.g. the data sorted above would look like. 3.8.4 Step 10: 3.8.4 Step 90: 3.8.4 Step 100: 3.8.10 Step 20: 3.9.1 Step 10: 3.9.1 Step 20: 3.10.2 Step 10: I have found the way to sort by first number on this site:

How to delete all the lines after the last occurence of pattern?

给你一囗甜甜゛ 提交于 2021-02-04 07:25:08
问题 i want to delete all the lines after the last occurence of pattern except the pattern itself file.txt honor apple redmi nokia apple samsung lg htc file.txt what i want honor apple redmi nokia apple what i have tried sed -i '/apple/q' file.txt this deletes all the line after the first occurence of pattern - honor 回答1: Simple, robust 2-pass approach using almost no memory: $ awk 'NR==FNR{if (/apple/) hit=NR; next} {print} FNR==hit{exit}' file file honor apple redmi nokia apple If that doesn't

How to delete lines before a match perserving it?

怎甘沉沦 提交于 2021-01-29 20:50:38
问题 I have the following script to remove all lines before a line which matches with a word: str=' 1 2 3 banana 4 5 6 banana 8 9 10 ' echo "$str" | awk -v pattern=banana ' print_it {print} $0 ~ pattern {print_it = 1} ' It returns: 4 5 6 banana 8 9 10 But I want to include the first match too. This is the desired output: banana 4 5 6 banana 8 9 10 How could I do this? Do you have any better idea with another command? I've also tried sed '0,/^banana$/d' , but seems it only works with files, and I

A UNIX Command to Find the Name of the Student who has the Second Highest Score

╄→гoц情女王★ 提交于 2021-01-29 20:08:55
问题 I am new to Unix Programming. Could you please help me to solve the question. For example, If the input file has the below content RollNo Name Score 234 ABC 70 567 QWE 12 457 RTE 56 234 XYZ 80 456 ERT 45 The output will be ABC I tried something like this sort -k3,3 -rn -t" " | head -n2 | awk '{print $2}' 回答1: Using awk awk 'NR>1{arr[$3]=$2} END {n=asorti(arr,arr_sorted); print arr[arr_sorted[n-1]]}' Demo: $cat file.txt RollNo Name Score 234 ABC 70 567 QWE 12 457 RTE 56 234 XYZ 80 456 ERT 45

How to use Awk to create a new field but retain the original field?

喜你入骨 提交于 2021-01-29 11:04:21
问题 Can this be done in Awk? FILE_IN (Input file) ID_Number|Title|Name 65765765|The Cat Sat on the Mat|Dennis Smith 65765799|The Dog Sat on the Catshelf|David Jones 65765797|The Horse Sat on the Sofa|Jeff Jones FILE_OUT (Desired Results) ID_Number|Title|Nickname|Name 65765765|The Cat Sat on the Mat|Cat Sat|Dennis Smith 65765799|The Dog Sat on the Catshelf|Dog|David Jones 65765797|The Horse Sat on the Sofa||Jeff Jones Logic to apply: IF Title contains “ Cat Sat ” OR " cat sat " THEN Nickname =

awk transpose lines based on pattern and move (copy) before current columns with filling empty fields

喜你入骨 提交于 2021-01-29 08:14:55
问题 Hi i am tring to move with this below formatted text input: NICK JEFF NOAA A4:80:15 NOAA A4:80:17 NOOI D0:F2:0B BASE-TREE_IN-M_K NICK STAN NOAA C1:46:6B NOOI D5:75:0C BASE-TREE_OUT_OUT NICK INDEXER NOOI D5:75:0C SEAT_25 NOAA C1:46:6B NICK VUZER NOAA A4:F2:CD NOOI D0:F2:2D SEAT_42_FLIGHT NOAA A4:F2:CD NICK CAPTAIN NOOI A4:82:8D NOAA A4:82:8F NOOI 40:63:07 SYS-BRAVO_X1 NOOI 40:62:DB SYS-BRAVO_X2 NOOI 40:62:B5 SYS-BRAVO_X3 NOOI D0:47:4A BASE-TREE_OUT_OUT NOOI 51:30:45 NOBASE-INDEX_OUT_FIF NOOI

Merge two rows of a file

徘徊边缘 提交于 2021-01-29 07:35:45
问题 I have a input file which has large data in below pattern. some part of data is shown below: Data1 C In; CP In; D In; Q Out; Data2 CP In; D In; Q Out; Data3 CP In; CPN In; D In; QN Out; I want my output as Data1(C,CP,D,Q) In C; In CP; In D; Out Q; Data2 (CP,D,Q) In CP; In D; Out Q; Data3 (CP,CPN,D,QN) In CP; In CPN In D Out QN; I tried code given in comment section below, But getting error. Corrections are welcome. 回答1: variation on @EdMorton suggestion - fixing the desired order of fields: $