Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature suggestion: Insert fields with constant values #16

Open
ldcasillas-progreso opened this issue Jul 11, 2015 · 4 comments
Open

Feature suggestion: Insert fields with constant values #16

ldcasillas-progreso opened this issue Jul 11, 2015 · 4 comments

Comments

@ldcasillas-progreso
Copy link
Contributor

A feature that I don't see in 0.9.6 that might be useful to add: insert new fields into a data set with a constant value.

Example use case: you have a set of files, each of which has data that pertains to a different date. The dates are in the file names but not in any field in the file. You would like to merge them into one data set, but still be able to tell which record came from which file. So you'd do something like:

for date in `generate-dates 2015-04-01 2015-06-30`
do
    xsv insert columns file_date:"$date" file_${date}.csv > file_${date}_with_file_date.csv
done

xsv cat rows file_*_with_file_date.csv > file_2015Q2.csv
@BurntSushi
Copy link
Owner

I can see how this might be occasionally useful.

@junoatwork
Copy link

Seconding the request this insert ability, and expanding on the use case for insert row. Something I currently do occasionally with jq is use it to generate properly-formatted JSON; I would love to be able to do something similar for CSV with the guarantee of all the quoting and escaping.

For example, with jq, I might do:

JSON_TEMPLATE='{$filePath, $size, $mtime}'
JSON=$(
    jq -n -c\
    --arg 'filePath' "$PATH" \
    --arg 'size' "${SIZE}" \
    --arg 'mtime' "${MTIME}" \
    "$JSON_TEMPLATE"
  )

to generate a line like {"filePath": "...", "size": "...", "mtime": "..."}

Going off of the syntax suggested by the OP, this could be something like:

xsv insert row filePath:"$PATH" size:"$SIZE" mtime:"$MTIME" > file.csv

or even something lower-level, like

xsv format "$PATH" "$SIZE" "$MTIME" >> file.csv

to allow for specifying various options (delimiter, line endings, column types, etc).

I'd be okay hearing that this is out of scope for this project, but it feels like it would be nice to have in a CLI "toolkit"

@dysinger

This comment has been minimized.

@opax
Copy link

opax commented Jan 17, 2023

Not as convenient and requires an extra file, but this is already possible with a cross join:

cat > table.csv <<EOF
a,b
1,2
2,4
EOF

# file for fixed column
cat > fixed.csv << EOF
c
FIXED
EOF

# add the column to table.csv
xsv join --cross 1 table.csv 1 fixed.csv

This results in the following output:

a,b,c
1,2,FIXED
2,4,FIXED

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants