Linux text only one

With the Linux «cat» command, how do I show only certain lines by number

If I use cat -n text.txt to automatically number the lines, how do I then use the command to show only certain numbered lines.

6 Answers 6

$ cat file Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 Line 9 Line 10 

To print multiple lines (5 & 8)

$ sed -n -e 5p -e 8p file Line 5 Line 8 

To print specific range (5 — 8)

$ sed -n 5,8p file Line 5 Line 6 Line 7 Line 8 

To print range with other specific line (5 — 8 & 10)

$ sed -n -e 5,8p -e 10p file Line 5 Line 6 Line 7 Line 8 Line 10 

Note that for a range including the first line (for example first 5 lines), you’ll use sed -n 1,5p . not sed -n 0,5p . . At least for the version of sed installed by default on Mac OS 10.15.

One way of doing it is by using sed :

where 11 is the number of the line you want removed.

And cat -n isn’t even needed:

You can use awk straight up.

replacing ‘1’ with the desired line number.

I concur that this is the very best answer. To add on top you can use equivalence operators to filter data out ie. awk ‘NR>=20’ file.txt

Depending on goals I like head or grep

cat /var/log/syslog -n | head -n 50 | tail -n 10

will return lines 41 thru 50.

cat /var/log/syslog -n | grep » 50″ -b10 -a10

will show lines 40 thru 60. The problem with the grep method is that you have to use account for padding of the line numbers (notice the space)

Both are quite handy for parsing log files.

well yeah. but. but. . There are better ways. The question asked about using cat though, so I used it.

As others have shown you, there is no need to use cat -n . Other programs will do it for you. If, however, you really need to parse the output of cat -n and show only specific lines (for example, 4-8, 12 and 42), you could do:

Читайте также:  User configuration file in linux

In awk , $1 is the first field, so this command prints all lines whose first fields are i) between 4 and 8 (inclusive) or ii) 12 or iii) 42.

If you also want to remove the field added by cat -n to get the original lines from the file, you can do:

$ cat -n file | awk '$1>=4 && $1' Line 4 Line 5 Line 6 Line 7 Line 8 Line 12 Line 42 

Источник

How to find the particular text stored in the file «data.txt» and it occurs only once

The line I seek is stored in the file data.txt and is the only line of text that occurs only once. How do I go about finding that particular line using linux?

Could you please give an example of what the text page looks like? Just replace the password with some dummy text if you will as well please.

Do I understand correctly, that there is only one line of text in data.txt? Then you just do cat data.txt .

7 Answers 7

This is a little bit old, but I think you are looking for this.

cat data.txt | sort | uniq -u 

This will show the unique values that only occur once in the file. I assume you are familiar with «over the wire» if you are asking?? If so, this is what you are looking for.

Was wondering why i had to use sort . read uniq ‘s manual again and found out it compares to adjacent lines. thats why, thank you! Oh, and sort -u didn’t work out, had to use the first command.

To provide some context (I need more rep to comment) this is a question that features in an online «wargame» called Bandit that involves using the command line to discover passwords on an online Linux server to advance up the levels.

For those who would like to see data.txt in full I’ve Pastebin’d it here however it looks like this:

NN4e37KW2tkIb3dC9ZHyOPdq1FqZwq9h jpEYciZvDIs6MLPhYoOGWQHNIoQZzE5q 3rpovhi1CyT7RUTunW30goGek5Q5Fu66 JOaWd4uAPii4Jc19AP2McmBNRzBYDAkO JOaWd4uAPii4Jc19AP2McmBNRzBYDAkO 9WV67QT4uZZK7JHwmOH0jnhurJMwoGZU a2GjmWtTe3tTM0ARl7TQwraPGXgfkH4f 7yJ8imXc7NNiovDuAl1ZC6xb0O0mMBx1 UsvVyFSfZZWbi6wgC7dAFyFuR6jQQUhR FcOJhZkHlnwqcD8QbvjRyn886rCrnWZ7 E3ugYDa6Wh2y8C8xQev7vOS8O3OgG1Hw E3ugYDa6Wh2y8C8xQev7vOS8O3OgG1Hw ME7nnzbId4W3dajsl6Xtviyl5uhmMenv J5lN3Qe4s7ktiwvcCj9ZHWrAJcUWEhUq aouHvjzagN8QT2BCMB6e9rlN4ffqZ0Qq ZRF5dlSuwuVV9TLhHKvPvRDrQ2L5ODfD 9ZjR3NTHue4YR6n4DgG5e0qMQcJjTaiM QT8Bw9ofH4x3MeRvYAVbYvV1e1zq3Xim i6A6TL6nqvjCAPvOdXZWjlYgyvqxmB7k tx7tQ6kgeJnC446CHbiJY7fyRwrwuhrs 

One way to do it is to use:

The sort command is like cat in that it displays the contents of the file however it sorts the file lexicographically by lines (it reorders them alphabetically so that matching ones are together).

Читайте также:  Linux disk check gui

The | is a pipe that redirects the output from one command into another.

The uniq command reports or omits repeated lines and by passing it the -u argument we tell it to report only unique lines.

Used together like this, the command will sort data.txt lexicographically by each line, find the unique line and print it back in the terminal for you.

Источник

Grep text only only after [word]:

On each [myword] it prints all after brackets, but how can I get only the one I need only and not all? I need to print only the test that after certain brackets. For example I want to print only text that in line [one]: but not after [two]: or [onemore]: lines, so that the output will be bla bla bla onebla twobla . If I want to print all after [onemore]: — the correct output should be i got mad and etc

3 Answers 3

With GNU grep with support for recent PCRE, you can also do:

In any case, note that in most shells, [. ] are glob operators. In grep [myword] , [myword] is expanded to the list of files that match that, that is any file in the current directory whose name is m , y , w , o , r or d (and if there’s none, depending on the shell, the pattern is passed as-is to grep , or you get an error). So they must be quoted for the shell (with single quotes for instance as in the solutions here). For instance, if there’s a file called r in the current directory, and one called d , grep [myword] would become grep d r in all shells but fish .

[. ] is also a special operator in regular expressions (very similar to the [. ] glob operator), grep ‘[myword]’ would match on lines that contain m , y , w , o , r or d . So you need to escape the opening [ for grep (for regular expressions) as well. That can be done with grep ‘\[myword]’ or grep ‘[[]myword]’ .

^ is another regular expression operator that means: match at the beginning of the line only. So grep ‘^\[myword]: ‘ matches on lines that start with [myword]: .

While grep is just meant to print matching lines (is not otherwise a stream editor like sed is), GNU grep added the non-standard -o option for it to print the matching portion(s) of the line (if non-empty). It also added the -P options to use perl compatible regular expressions (in PCRE) instead of basic ones without -P .

Читайте также:  Hp m227sdn драйвер linux

In recent PCREs, \K is an operator that resets the start of the matching portion. So in grep -Po ‘^\[one]: \K.*’ , we do print the matching portion because of -o , but because of \K , that matching portion becomes the sequence of characters ( .* ) that is found after [one]: .

Источник

how can I print or display only word from a text file at a time. When I run my script it should only display each word from the text file one by one, but only display that word. This is what I have done, but it displays each word on separate line, but it shows all of them:

FS=$'\n' for j in `cat read` do echo "$j" done 
root@matrix:~> first word ---> this word disappear when second is displayed root@matrix:~> second word ---> this disappear when third is displayed root@matrix:~> third word ---> this disappear when fourth is displayed and continues like this to the end of the file! 

2 Answers 2

You can use sleep and clear commands in your script as following:

Explanation:

The sleep command make delay for a specified amount of time (in seconds). With sleep 1 delay would be for 1 second. You can change for more time delay by incrementing the second parameter or for delaying less than 1 second divide it to low units; Like sleep .1 for 1/10 second delay or sleep .001 for 1/1000 second delay and etc.

The clear command clear the terminal screen.

Even better you can do this through below awk command:

Explanation:

In awk , the NF specifies the total number of fields in the current input record/line, so by using a variable as a counter ( i ) and looping over it, we are printing all of them from 1st position to the end of them ( NF ). Then by using the system(«sleep 1; clear») part, we are telling to awk to calling the system commands to sleeping for 1 second and clearing the screen.

In above we are displaying the input file as word by word. If you are going to display it line by line add IFS=$’\n’ in the script like:

And change the awk command like:

Источник

Оцените статью
Adblock
detector