New answers tagged

0

An additional bash solution: It will handle unmatched pairs of braces {. Won't accept a closing brace until one or more opening braces appear. Will reset the count of braces to 0 at the end of the line. Will accept a comma as valid after more closing braces than opening braces. Will remove one space in front of solution. Will quote the resulting word. ...


0

A skeleton, search the web for more: #!/usr/bin/python import csv # implement your pseudo-code here if you have some problem, come again and ask us. Related : https://stackoverflow.com/questions/16503560/read-specific-columns-from-csv-file-with-python-csv It's not difficult and will be clearer and stronger than with awk IMHO EDIT: if your input ...


0

This perl code produces almost exactly the expected output: use Text::CSV; my $csv = Text::CSV->new({ binary => 1, eol => $/, allow_loose_quotes => 1, escape_char => undef }); open my $io, "<", $ARGV[0] or die; while (my $row = $csv->getline ($io)) { my @o = map { $_ =~ s,^\s*,,; $_ =~ s,\s*$,,; $_; } @{$row}; ...


2

Here's a sed script that will split your example: #!/bin/sed -Ef # replace all commas with newlines s/,/\ /g # Do we need to re-join any lines? :loop # Unmatched brace containing possibly another (matched) level of # braces: s/(\{([^{}]|\{[^{}]*\})*)\ /\1,/ tloop # remove any leading space s/\n */\ /g # At first line, print result, then exit. 1q ...


2

Try a pure bash #!/bin/bash TEST_STRING="{0,1}, alpha, {(x,y,z)}, {{1,2,3}, {a,b,c}}" TEST_STRING="$TEST_STRING""," count=0 newword='' while [ "${TEST_STRING::1}" ] ; do l="${TEST_STRING::1}" TEST_STRING=${TEST_STRING:1} [ "$l" = '{' ] && ((count++)) [ "$l" = '}' ] && ((count--)) if [ "$l" = ',' ] && ! ((count)) ...


0

You can do the following: find -name "data.csv" | xargs -n 1 sed -n 2p >> final.csv Note above that using >> concatenates results from sed into final.csv, on a new line, instead of replacing them, whereas > simply replaces content in final.csv with output from sed.


0

The result of the find is the list of filenames that sed needs to act upon. Thus use xargs to execute sed on that list, one by one. But because filenames can contains spaces and newlines even, better use the "-print0" option of find, to delimit the filenames with ascii zero. Also, when those files are big, you can save cpu-heat by stopping after line 2. ...


1

If you look at the output of find -name "data.csv", you'll see that it prints the full pathnames of files named data.csv. The pipeline passes that on to sed, which prints the second line of its input. So what you're saying is, "Here's a list of files. Give me the second one one the list." What you really want to say is "Here's a list of files. For each one, ...


8

To try and avoid storing the whole file in memory, you could do: awk -F , ' !count[$1]++ {save[$1] = $0; next} count[$1] == 2 { print save[$1] delete save[$1] } {print}'


0

Another variant (where test.txt is your input file): FILE=test.txt ; for n in $(cat ${FILE} | awk -F"," '{count[$1]++} END {for (i in count) print i":"count[i]}'|grep -v ':1'|awk -F: '{print $1}');do grep ^${n} ${FILE} ;done


1

With awk (GNU awk for multi-dimensional arrays) gawk -F, ' { line[NR] = $0; count[$1]++; found[$1][NR] = 1} END { for (id in count) if (count[id] > 1) for (nr in found[id]) print line[nr] } ' file The order of the output may not be the same as the input file.


0

for i in $(cat given | cut -d, -f1) do linect=$(grep ^"${i}," given | wc -l) if [ ${linect} -gt 1 ] then grep ^"${i}," given >> result fi done sort result |uniq > desiredoutput as long as the fields are delimited by comma and you are seeking duplicates in the column 1 and column 1 only, this should work.


3

Perl solution: perl -F, -ane ' $h{ $F[0] } .= $_ }{ $h{$_} =~ tr/\n// >= 2 and print $h{$_} for keys %h ' < input-file -n reads the input line by line -a splits each line on -F, i.e. comma, into the @F array. lines are stored in the %h hash keyed by the first field ($F[0]). They are concatenated ...



Top 50 recent answers are included