Skip to content

Tag: awk

Group the consecutive numbers in shell

In shell, how to group the numbers in $foo as 1-3,6-8,11,13-17 Answer As an alternative, you can use this awk command: Now run it as: Here is an one-liner for doing the same: In this awk command we keep 2 variables: p for storing previous line’s number s for storing start of the range that need to be pr…

edit the only lines which matches some word

I have to search a file for lines with some specific words and edit these lines as follows. The other lines should remain the same. For example: to be modified to: Can any one please help how to use AWK to achieve this. Regards Answer awk solution: Explanation: When brz or jp matches, save last field in last,…

Shell script to edit and add a entry in file

Could any one help me to add entry at the end of the line. This is linux os. Below is the contents of the file /boot/grub2/grub.cfg I need to add entry at the end of the line “text1” and “text2”. after edit my file should look like: Answer With sed expression:

Awk in loopwith different file

I have four files and want to run a awk code on each file. But this code is not working. My code: Answer Awk can work with multiple files no need of for syntax will be like this or In your case To correct your existing code change To

extract multiple blocks of text to a new file

I need help extracting blocks of information from a single text file into new files or some form where I can manipulate the data from each block. Example: I need to be able to display and/or manipulate the variables (which I can do if I can isolate the right one). I don’t mind writing each one of these …

Sed/awk: Aligning words in a file

I have a file with the following structure: I want to add another item to the file, using sed or awk: etc. So my next item looks like this: The column values is jagged. How do I align my values to the left like for previous items? I can see 2 solutions here: To align the values while inserting them