Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a file containing lines as below.
#user
codecodecodecodecodecodecodecode
codecodecodecodecodecodecodecode
codecodecodecodecodecodecodecode
codecodecodecodecodecodecodecode;
#user1
code1code1code1code1code1code1
code1code1code1code1code1code1
code1code1code1code1code1code1
code1code1code1code1code1code1;
#user2
code2code2code2code2code2code2
code2code2code2code2code2code2
code2code2code2code2code2code2
code2code2code2code2code2code2;
#user (again "user" but with a different code)
code3code3code3code3code3code3
code3code3code3code3code3code3
code3code3code3code3code3code3
code3code3code3code3code3code3;
I want extract only codes from the "user", the output I'm looking for is:
#user
codecodecodecodecodecodecodecode
codecodecodecodecodecodecodecode
codecodecodecodecodecodecodecode
codecodecodecodecodecodecodecode;
#user
code3code3code3code3code3code3
code3code3code3code3code3code3
code3code3code3code3code3code3
code3code3code3code3code3code3;
Results retuned only the lines matching "user" and its respective codes.
I tried awk -F";" '{print $1}' $file but i cant isolate codes from a specific user.
In a perl one liner:
perl -ne 'print if /^#/' in.txt
You can use awk as follows:
awk -F\; '/code/' test.txt
Assuming the field delimiter is ; and the pattern is code.
If you want to print a particular line or column, use awk's NR and NF variables respectively.
For more info: http://www.gnu.org/software/gawk/manual/gawk.html
Related
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 6 years ago.
Improve this question
I'm interested in using command line (possibly Perl) to generate a list of all possible IP addresses.
I've done similar with PHP in the past by using the long2ip function and creating a list from 0 to the interger 4294967295.
Is there a way to do this in Perl instead though?
I'm basically just looking for the quickest way to generate a text file that has a list of all 4,294,967,296 possible IP addresses.
There is no need to use any modules. This is a trivial problem.
for my $i (0..255) {
for my $j (0..255) {
for my $k (0..255) {
for my $l (0..255) {
printf("%d.%d.%d.%d\n", $i,$j,$k,$l)
}
}
}
}
One-liner time?
perl -MSocket=inet_ntoa -le 'print inet_ntoa(pack "N", $_) for 0..2**32-1'
Source: http://www.perlmonks.org/?node_id=786521 via quick googling.
Perl isn't strictly necessary either, of course. The following generates a quick sed script on the fly and calls it successively.
octets () { sed "h;$(for ((i=0; i<256; i++)); do printf "g;s/^/$i./p;"; done)"; }
octets <<<'' | octets | octets | octets | sed 's/\.$//'
The octets function generates 256 copies of its input with a (zero-based) line number and a dot prepended to each. (You could easily append at the end instead, of course.) In the sed scripting language, the h command copies the input to the hold space and g retrieves it back, overwriting whatever we had there before. The C-style for loop and the <<< here string are Bash extensions, so not POSIX shell.
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 8 years ago.
Improve this question
How can I use sed to remove the last character from only the first line of a file?
You can for example use this:
sed '1 s/.$//' file
Explanation
1 indicates the line in which we want to perform the action.
given the syntax s/text/replacement/, we look for any character with . followed by $, which indicates end of line. Hence, we look for the last character before end of line and replace it with nothing. That is, we remove the last character of the line.
To edit the file you can use -i.bak.
Test
$ cat a
hello this is some text
and this is something else
$ sed '1 s/.$//' a
hello this is some tex
and this is something else
For fun, let's see how to accomplish this with awk:
awk -v FS= -v OFS= 'NR==1{NF=NF-1}1' file
This sets the input and output field separators (FS, OFS) as empty (same as BEGIN{FS=OFS=""}), so every single character is a field. Based on that, when the record is 1 (in this case, when we are in the 1st line), decrement the number of fields (NF) so that the last character is "lost". Then 1 is a true condition that makes awk perform its default action: {print $0}.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a file of e-mail addresses harvested from Outlook so that the addresses in the harvested form show up like this:
-A#b.com
-C#d.com
-A#b.com,JOHN DOE, RICHARD ROE,"\O=USERS:SAM"
etc.
What I would like to end up with is a text file that has one validly formed address on each line. So A#b.com would be OK, but "RICHARD ROE" and the "\O=USERS,etc." would not be. Perhaps this could be done with SED or AWK?
Here's one way with GNU awk given your posted input file:
$ gawk -v RS='[[:alnum:]_.]+#[[:alnum:]_]+[.][[:alnum:]]+' 'RT{print RT}' file
A#b.com
C#d.com
A#b.com
It just finds simple email addresses, e.g. "bob#the_moon.net" or "Joe.Brown#google.com", feel free to change the setting of RS if you can figure out an appropriate RE to capture the more esoteric email addresses that are allowed or post a more representative input file if you have examples. here's another RE that works by specifying what character cannot be in the parts of an email address rather than those that can:
$ gawk -v RS='[^[:space:][:punct:]]+#[^[:space:][:punct:]]+[.][^[:space:][:punct:]]+' 'RT{print RT}' file
A#b.com
C#d.com
A#b.com
Again it works with your posted sample, but may not with others. Massage to suit...
With other awks you can do the same by setting FS or using match() and looping.
You can try:
awk -F, '{
for (i=1; i<=NF; i++)
if ($i ~ /#/)
print $i
}' file
or like this:
awk -F, -f e.awk file
where e.awk is:
{
for (i=1; i<=NF; i++)
if ($i ~ /#/)
print $i
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a data file that looks like this
15105021
15105043
15106013
15106024
15106035
15105024
15105042
15106015
15106021
15106034
and I need to grep lines that have sequence numbers like 1510603, 1510504
I tried this awk command
awk /[1510603,1510504]/ soursefile.txt
but it does not work.
Using egrep and word boundary on LHS since OP wants to match all matching numbers on RHS:
egrep '\b(1510603|1510504)' file
15105043
15106035
15105042
15106034
An shorter awk
awk '/1510603|1510504/' file
Based on the contents of your file the following should suffice
grep -E '^1510603|^1510504' file
If your grep version does not support the -E flag, try egrep instead of grep
If you insist on awk
awk '/^1510603/ || /^1510504/' file
Think this works:
egrep '1510603|1510504' source
Your question is very poorly stated, but if you want to print all numbers in the file that begin with either 1510603 or 1510504, then you can write this in Perl
perl -ne 'print if /^1510(?:603|504)/' sourcefile.txt
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm looking for a quick and efficient way to double quote all fields in tab delimited or comma separated text files.
Ideally, this would be a Perl one-liner that I can run from the command-line, but I'm open to any kind of solution.
Use Text::CSV:
perl -MText::CSV -e'
my $c = Text::CSV->new({always_quote => 1, binary => 1, eol => "\n"}) or die;
$c->print(\*STDOUT, $_) while $_ = $c->getline(\*ARGV)' <<'END'
foo,bar, baz qux,quux
apple,"orange",spam, eggs
END
Output:
"foo","bar"," baz qux","quux"
"apple","orange","spam"," eggs"
The always_quote option is the important one here.
If your file does not contain any double quoted strings containing the delimiter, you can use
perl -laF, -ne '$" = q(","); print qq("#F")'
awk -F, -v OFS='","' -v q='"' '{$0=q$0q;$1=$1}7' file
for example, comma sep:
kent $ echo "foo,bar,baz"|awk -F, -v OFS='","' -v q='"' '{$0=q$0q;$1=$1}7'
"foo","bar","baz"
tab sep would be similar.