Tag Info

New answers tagged

2

The question has been edited to include data in a totally different format. The original answer is below the line, and the parts of it relating to sed in general still apply. sed can rewrite lines into other formats. Here's a quick sed command to give you a CSV line per record for this new data: sed -n -e '/secs:/{s/.*secs://;H;};/position:/{s/^.*position: ...


3

The command awk -F, 'NR==1 { for (i=1; i<=NF; i++) sn[i]=$i } NR==2 { for (i=1; i<=NF; i++) print sn[i] "=" $i; exit }' states where states is your file, will output Alabama=1000 Alaska="1 0 0 1" Arizona=1002 Arkansas=1002 California=1003 Colorado=1004 Connecticut=1005 Delaware="1 0 0 6" Florida=1007 Georgia=1008 Hawaii=1009 Idaho=1010 ...


1

I am probably a little bit too late, but there is another tool worth mentioning: csvkit http://csvkit.readthedocs.org/ It has a lot of command line tools that can: convert to and from csv from various formats (json, sql, xls) cut, grep, sort and others join different csv files!


0

TXR language: @(do (defun csv-parse (str) (let ((toks (tok-str str #/[^\s,][^,]+[^\s,]|"[^"]*"|[^\s,]/))) [mapcar (do let ((l (match-regex @1 #/".*"/))) (if (eql l (length @1)) [@1 1..-1] @1)) toks])) (defun csv-format (list) (cat-str (mapcar (do if (find #\, @1) `"@1"` @1) list) ", ")) ...


2

Use this line: read `sed -e 's/,/ /g' -e '1q;d' file` < <(sed -e 's/\ /\\\ /g' -e 's/"//g' -e 's/,/ /g' -e '2q;d' file) Explanation: The first sed command causes that only the first line of the file is printed and commas (,) are replaced with spaces: Alabama Alaska Arizona Arkansas California ... The second sed command does the same with the ...


0

Obviously, using a csv parser would be better but if we can safely assume that The 1st field will never contain a comma; You only want the ids that are present in the 1st file (if an id is in file2 or file3 and not in file1 of you ignore it); The files are small enough to fit in your RAM. Then this Perl approach should work: #!/usr/bin/env perl use ...


1

I assume that by csv_date_x you mean csv_2014_4_15 and csv_date_x+1 means csv_2014_4_16. But according to your pseudo-code you never use any lines from csv_date_x, so that's more a filter than a merge and you wind up with a subset of csv_2014_4_16. This is a non-trivial programming task, but I can point you in a profitable direction. This sort of thing can ...


2

Try this: $ awk -F',' -v t=$(date +"%Y-%-m-%-d) ' FNR == NR { u[$1] = $4; next; } $4 > u[$1] { print; next; } t ~ $3 ' file[x] file[x+1] Explanation We get today date, save it in variable t While reading file[x] FNR == NR, we save each updated time of each id in associative array u, key is id, value ...


2

You should be able to do the basic merge using two applications of the coreutils join command if your system has it. If your shell supports process substitution you can do that as join -t, <(join -t, csv1 csv2) csv3 (otherwise you may need to use an intermediate temporary file). Using your csv1, csv2 and csv3 files as posted, that gives: $ join -t, ...



Top 50 recent answers are included