Monday, January 31, 2022

[SOLVED] How to delete rest of the line after matching pattern?

Issue

I have several files whose contents are like below:

*****;ABCD
*****;XYZ
*****;HPSD hello
*****;EFGH hi

After the ";"(semicolon) i want to retain the word but delete anything that comes after (hello & hi) How do i do this ?


Solution

Using awk:

awk -F\; '{ split($2,map," ");print $1FS map[1]}' file

Set ";" as the field delimiter and then split the second field into the array map, using space as the delimiter. Print the first field, the field separator (;) and then the first index of map.



Answered By - Raman Sailopal
Answer Checked By - Mildred Charles (WPSolving Admin)