basically I want to list one of every value like this.
data I have:
aaa
aaa
bbb
aaa
ccc
bbb
ddd
ccc
ddd
aaa
the output that I want:
aaa
bbb
ccc
ddd
how can I do this?
I can't find anything on google because I don't know the right keyword to search.
Related
This question already has answers here:
Extract email addresses from text file using regex with bash or command line
(4 answers)
Closed 2 years ago.
How would I extract the email addresses from a file like below where the email addresses are in different columns and then output this to a file
aaa jim#gmail.com bbb ccc
aaa bbb john#gmail.com ccc ddd
aaa joe#gmail.com' bbb ccc
etc
This can easily be done, using grep -o: the -o only shows the result of the regular expression. As far as the regular expression for an e-mail address is concerned, you might try this:
grep -o "[a-zA-Z0-9.-]+#[a-zA-Z0-9.-]+.[a-zA-Z0-9.-]+" filename.txt
I'm trying to sort a text file in this manner:
6 aaa
4 bbb
2 ccc
2 ddd
That is, each line sorted first in numeric descending order (the number indicates the number of occurrences of the word on the right), and if multiple words are repeated the same number of times, I'd like to have those words sorted alphabetically.
What I have:
6 aaa
4 bbb
2 ddd
2 ccc
When I try sort -nr | sort -V it kind of does what I want but in ascending order.
2 ccc
2 ddd
4 bbb
6 aaa
What's a clean way to accomplish this?
I think you just need to specify that the numeric reverse sort only applies to the first field:
$ sort -k1,1nr file
6 aaa
4 bbb
2 ccc
2 ddd
-k1,1[OPTS] means that OPTS only apply between the 1st and 1st field. The rest of the line is sorted according to global ordering options. In this case, since no other options were passed, this means the default lexicographic sort.
Maybe using tac? (not a shell expert here, just remembering uni days...
sort -nr | sort -V | tac
I have a file with these lines:
aaa
aaa
aaa
aaa
aaa
aaa
bbb
bbb
bbb
bbb
bbb
bbb
ccc
ccc
ccc
ccc
ccc
ccc
ddd
ddd
ddd
ddd
ddd
ddd
i want to convert this into:
aaa;100;
aaa
aaa
aaa
aaa
aaa
bbb;100;
bbb
bbb
bbb
bbb
bbb
ccc;100;
ccc
ccc
ccc
ccc
ccc
ddd;100;
ddd
ddd
ddd
ddd
ddd
Is this possible in vim in one command ?
Yet another one
:g/^/ if !((line('.')-1)%6)|s/$/;100;
Breakdown
g/^/ Global command to apply next expression on each line
if !((line('.')-1)%6) If the modulus of the current line equals 0
s/$/;100; Replace the line ending with ;100;
That depends on what you mean by "one command", but you can do without manually repeating it for each item by using a macro:
Position your cursor on the first line
Start recording a macro named z: qz
Enter insert mode at the end of the line: <shift-A>
Enter the text you want: ;100;
Exit insert mode: <esc>
Jump down six lines: 6j
Stop recording the macro: q
Repeat the macro the right number more times: 3#z
Because the jumping down 6 lines is part of the macro, it will line up properly and loop through the file.
The relevant commands here are q# to start recording a macro, q to end the recording, and ## to play a recording back.
More information can be found in various places, such as the vim wiki: http://vim.wikia.com/wiki/Macros
If the lines are all the same until the change, this is a pretty nasty Vim solution:
/\v(.*)\n\zs\1#!
:%g//norm A;100;
Traditionally in Vim you craft a search first, then use :%s//replace to replace your last search with "replace". In one line it would be:
:%g/\v(.*)\n\zs\1#!/norm A;100;
I'm so sorry. This is what happens after years of Vim use. It's not pretty.
Explanation:
Essentially we're finding lines that AREN'T duplicated on the next line, and performing an action on them, in this case, adding some text.
:%g/ Perform an action on a pattern (same syntax as %s)
\v Very magic. Makes all special characters in Vim regexes special.
(.*)\n any text followed by a line break. Capture the text.
\zs Start highlighting the match here. This will put the cursor on the next line after the match, where we will perform the action.
\1 The capture group from above (.*), so a new line with the same text...
#! But negate it! So the cursor will go to any line that is NOT duplicated by the previous line.
/norm A;100; Peform the normal mode command A;100; which will execute keystrokes on each line as if you were in normal mode. Regular Vim here, just append (A) text.
:%s/\v(.*)(\n.*)(\n.*)(\n.*)(\n.*)(\n.*\n)/\1;100;\2\3\4\5\6/
A simple problem that I do not have the right terminology to google to.
I am in visual mode when I delete some lines:
aaa
eee
fff
bbb
ccc
ddd
ggg
hhh
from first whitespace in 'b' row I press $ to select to end of line and then down to 'd' line but when I put them in the slot after 'aaa' I get this:
aaa
bbb
ccc eee
ddd fff
ggg
hhh
I want 'e' and 'f' lines to push down, not out. I can do it by copy/paste using the mouse in insert mode but the mouse is not my friend so I would like to know another way of doing it.
It looks like you are using "visual-block mode" (<C-v>, or <C-q> if you source mswin.vim, which you shouldn't) instead of the correct "visual-line mode" (V).
Alright, I have a spreadsheet that looks like this in the "B" column:
AAA
BBB
BBB
AAA
BBB
CCC
DDD
BBB
AAA
BBB
CCC
What I need to do is to count how many times "BBB" directly follows "AAA" (In this example 3 times)
Ive tried multiple things with =SumProduct like =SUMPRODUCT(COUNTIFS(K6:K10,{"AAA","BBB"})) but that returns the product of all "AAA's" and "BBB's" instead of just the number of AAA & BBB pairs. The best I could do is to use =countifs() but I cant see a way to do a forward lookup like =countifs("B:B","AAA","B+1:B+1","BBB")
Also, I should mention, I was hoping to use this somewhere in the formula "Table13[[#All],[Radio State]]." That way the formula would grow and shrink depending on the size of the table.
Does anyone happen to know if its possible to do this?
Thanks Guys,
You can 'offset' the range by a bit like this:
=COUNTIFS(A1:A10, "AAA", A2:A11, "BBB")
You can change the range accordingly.
With a table:
=COUNTIFS(Table13[[#All],[Radio State]],"AAA",OFFSET(Table13[[#All],[Radio State]],1,0),"BBB")