Replace files with sed

Asked

Viewed 537 times

0

Hi, I have a problem I need to solve. I have a file that should be inserted exactly one column of another file, I’m trying as follows:

cat $1 | cut -d ',' -f2 | tr "/" "-" | awk -F "-" '{print $2"-"$1"-"$3}' > temp.txt

cat $1 | sed 's/[0-3][0-9]\/[0-1][0-9]\/[0-9][0-9][0-9][0-9]/temp.txt/'

Of which temp.txt is the file that has the updated column. The problem is that instead of replacing the specified column, it is overwriting with the file name "temp.txt". Any suggestions to fix this? Or another way to do it? Thank you!

  • Can you explain better? I don’t quite understand your problem.

  • my problem is as follows: Change the column to "Year-Month-Day" Example: convert from 12/22/2012 to 2012-12-22

  • Edit the question....

  • I generated a file with the change called tempo.txt, in 12-22-2012 format. I need to insert this file in the second column of another file. csv

2 answers

2

Although the question is very strange (hint for the future, indicates what you want to do and not what you are doing, because you may be going the wrong way), I assume you want to change the date field of the US format (which is totally illogical, but they are like that). month/day/year for day-month-year.

Then this will be enough:

cat $1 | sed -r 's,([0-9]{2})/([0-9]{2})/([0-9]{4}),\2-\1-\3,g' 

"," is replacing the usual sed "/" to avoid escaping "/" and -r to use extended regexp

  • 1

    cool (+1) . eventually eliminate the cat: sed -r 's.......' $1

1

The solution proposed by @Iguita is perfect (solves the problem at once).

Yet answering directly to your sub-question : insert file in column 2 of a csv file, you can use the command Paste:

paste -d, <(cut -d, -f1 a.csv ) coluna2.txt <(cut -d, -f3- a.csv)
  • paste - side by side
  • -d, - column separator ,
  • <(cut -d, -f1 a.csv ) - extracts column 1 from the a.csv file
  • <(cut -d, -f3- a.csv) - extracts columns 3 onwards

Browser other questions tagged

You are not signed in. Login or sign up in order to post.