How can I use awk to display some certain fields in a text file? [duplicate] - linux

This question already has answers here:
How to print third column to last column?
(19 answers)
Closed 9 years ago.
I have a text file like this:
1 http 2 3 4 5
2 dns 3 4
3 ftp 3 4 5 6 8
I want the output to be like this:
http 2 3 4 5
dns 3 4
ftp 3 4 5 6 8
Node that I just want to omit the first column in that file and the fields number in a certain line is not fixed.
Can I accomplish this goal using awk?

You can also use cut: cut -d' ' -f2-.
Edit: If you have to use awk, try awk '{$1=""; print $0}'

something like this maybe?
awk '{$1 =""; print }' file

If you don't care about the the field separators remaining in place, you can do this:
awk '{$1=""}1' filename
(Assuming filename is where you stored your data)

Drats, I was going to give you an Awk solution, then recommend cut. It looks like others have beaten me to the punch.
However, I see no sed solution yet!
$ sed -n `s/^[^ ][^ ]*//p` yourfile.txt

sed 's/^..//g' your_file
above should work based on the condition that the first field is always of a single character.
or in perl:
perl -pe 's/^[^\s]*\s//g' your_file

Related

How to grep a word from a specific line till the end of file? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
How to find a word for eg
Find "abc" starting from line 6 till the end of file ?
You can grep the output of tail command like this.
tail -n+6 file.txt | grep abc
In sed, it's easy to say "ignore lines in this address range" and then grep in the rest.
sed -n -e '1,5d' -e '/abc/p' file
The -n option says to not print all lines by default, we then simply delete lines 1 through 5, and then print any matching lines in the remainder.
There's also a block syntax, so you can say
sed -n '6,${/abc/p;}' file
but the precise syntax differs slightly between dialects (I think Linux would not demand a semicolon before the closing brace?) The address range 6,$ selects lines from the sixth through the end of the file.
One solution is to use awk, checking both the text and the line number (this script contains only the rule to select lines, it uses the default action which is to print the line):
awk 'NR >= 6 && /abc/' inputfile.txt
The following transcript shows this in action:
pax:~> cat inputfile.txt
1 abc
2 def
3 abc
4 xxx
5 yyy
6 abc
7 xyz
8 abc
9 abc
pax:~> awk 'NR >= 6 && /abc/' inputfile.txt
6 abc
8 abc
9 abc
Delete (d) lines from 1 to 5 (1,5); and also delete (the other d) lines not (!) containing abc (/abc/):
sed '1,5d;/abc/!d' input.txt

Get text only within parenthesis from a file in linux terminal [duplicate]

This question already has an answer here:
How can I extract the content between two brackets?
(1 answer)
Closed 4 years ago.
I have a large log file I need to sort, I want to extract the text between parentheses. The format is something like this:
<#44541545451865156> (example#6144) has left the server!
How would I go about extracting "example#6144"?
This sed should work here:
sed -E -n 's/.*\((.*)\).*$/\1/p' file_name
There are many ways to skin this cat.
Assuming you always have only one lexeme in parentheses, you can use bash parameter expansion:
while read t; do echo $(t=${t#*(}; echo ${t%)*}); done <logfile
The first substitution: ${t#*(} cuts off everything up and including the left parenthesis, leaving you with example#6144) has left the server!; the second one: ${t%)*} cuts off the right parenthesis and everything after that.
Alternatively, you can also use awk:
awk -F'[)(]' '{print $2}' logfile
-F'[)(]' tells awk to use either parenthesis as the field delimiter, so it splits the input string into three tokens: <#44541545451865156>, example#6144, and has left the server!; then {print $2} instructs it to print the second token.
cut would also do:
cut -d'(' -f 2 logfile | cut -d')' -f 1
Try this:
sed -e 's/^.*(\([^()]*\)).*$/\1/' <logfile
The /^.*(\([^()]*\)).*$/ is a regular expression or regex. Regexes are hard to read until you get used to them, but are most useful for extracting text by pattern, as you are doing here.

ubuntu terminal: grep for numbers compare [duplicate]

This question already has answers here:
Is it possible to use egrep to match numbers within a range?
(2 answers)
Closed 6 years ago.
I have text file with table |ID | NAME | CREDIT| and content
Is it real to get all lines, where CREDIT < 1337(for example) by grep and ONLY with GREP, no awk or something else?
Have no idea, tnx
You can do it with pure grep, but it's ugly. Here you are:
grep -e " .$" -e " ..$" -e " ...$" -e " 1[0-2]..$" -e " 13[0-2].$" -e " 133[0-6]$"
This is a job very much unsuited to grep. As an artisan, you should select your tools carefully, no-one wants to try cutting down a giant Karri tree with a screwdriver :-)
It is almost certainly a job for awk. You haven't specified your content lines so let's assume for now they're of the form:
|iii|nnnnnnn|ccccc|
where the i, n and c sequences are the relevant column data.
To get those lines where the credit value is less than 1337, it's a simple matter to do:
awk -F'|' '$4 < 1337 {print}' inputFileName

insert underscore in columns of a text file shell [duplicate]

This question already has answers here:
Replace whitespace with a comma in a text file in Linux
(9 answers)
Closed 6 years ago.
I have a tab separated text file f.txt like :
APPLE 10 5
BALL 20 6
CAT 30 7
I want the output to be
APPLE_10_5
BALL_20_6
CAT_30_7
I wrote the following to partially accomplish this, but I am stuck at the "paste" step. Can you help?
cat f.txt | cut -f 1,2,3 | paste ???
When the are sperated by one space using sed is a one liner.
sed -i "s/ /_/" input.txt

Performing set operations using linux command [duplicate]

This question already has answers here:
difference between the content of two files
(4 answers)
Closed 8 years ago.
I have the following two lists of numbers stored in two different files:
File A:
7
1
2
9
File B:
10
8
4
9
Now I want to find out the set operation A-B (i.e. find only those numbers that are in A but not in B). Is there some way by which I may do the same using linux command (like sed). I know it is possible to do it using python, but I am just curious to know if it is possible to do the same using some linux command?
The simple, almost-working version is this:
grep -v -f file2 file1
That is, use lines from file2 as patterns; match them in file1 and print the ones not found (i.e. file1 - file2). However, what if file1 contains 10 and file2 contains 1? Then we have a problem because substrings are matched too. We can fix it this way:
grep -v -f <(sed 's/\(.*\)/^\1$/' file2) file1
That is, preprocess file2 to prepend ^ and append $ so the matching occurs against entire lines, not substrings in file1.
diff is the tool for this:
diff f1 f2
1,3c1,3
< 7
< 1
< 2
---
> 10
> 8
> 4
f1 has this unique number 7,1,2
f2 has this unique number 10,8,4

Resources