Convert .CSV file to .txt file using bash - bash

I have a CSV with lots of lines delimited by comma. I have to convert it to text.
66012523,39,Feb 02 2015 05:19AM,
66012523,39,Feb 02 2015 09:53AM,
66012523,39,Feb 02 2015 01:38PM,
I used command cp source.csv destination.csv and also cat source.csv > destination.txt but it does output in the same format witch each line coming in new one. It just gets appended together. It outputs like
66012523,39,Feb 02 2015 05:19AM,66012523,39,Feb 02 2015 09:53AM,66012523,39,Feb 02 2015 01:38PM
How do I make them to output each line in newline. Please help.

I hypothesise that the first block in your question is what you WANT, and what you actually HAVE is
66012523,39,Feb 02 2015 05:19AM,66012523,39,Feb 02 2015 09:53AM,66012523,39,Feb 02 2015 01:38PM
So what you want to do, I hypothesise, is split this up into separate lines.
Am I right?
This is a bit rough-and-ready but it works. Relies on each group ending "M," and there being no other "M,"s in the text - which there aren't but I wouldn't call it robust.
sed s/M,/'\n'/g source.csv

Related

Parse line for specific date format?

Writing a script using bash. I am trying to look through lines in a file for a specific date format:
date +"%a %b %d %T %Z %Y"
For example, if the line were
/foo/bar/foobar this 12 is 411 arbitrary stuff in the line Wed Jun 10 10:10:10 PST 2017
I would want to obtain Wed Jun 10 10:10:10 PST 2017.
Any way to search for specific date formats?
I'm not sure whether you'll agree with this approach. But if this is for some quick, non-recurring work, I won't look for a perfect solution that can handle all the scenarios.
To start with, you can use the following too generic pattern to match the part you want.
cat file | sed -n 's/.*\(... ... .. ..:..:.. ... ....\).*/\1/p'
Then you can enhance this further restricting the matches as you need.
E.g.
cat file | sed -n 's/.*\([a-Z]\{3\} [a-Z]\{3\} [0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9] [A-Z]\{3\} [0-9]\{4\}\).*/\1/p'
Note that this still is not perfect and can match invalid contents. If you find it still not good enough, you can further fine tune the pattern to the point you want.

invalid argument for option '-resize' in shell script

I've coded the following script in shell that creates a folder in a directory, then moves all images to this directory and then converts it and puts to a folder before. if I do this process manually it works fine, but apparently there's a conflict between the "convert" command and my loop. the "big" folder is the one that will retain the images with its original sizes and then the "tabloid" folder will contain thumbnail-sized formats of the same images.
cd tabloid
mkdir big
mv * big
cd big
for i in 00 01 02 03 04 05 06 07 08 09 10 11
do
convert -resize 351×383 "$i.jpg" "../$i.jpg"
done
it returns the following error:
convert.im6: invalid argument for option `-resize': 351×383 # error/convert.c/ConvertImageCommand/2382.
I don't know what could be wrong with my script. apparently it's in the loop because if I do the process manually (converting image per image via terminal), it works fine.
That's not an ASCII x (0x78) in your convert line that's × (MULTIPLICATION SIGN 0xd7).

Use cat to combine mp3 files based on filename

I have a large number of downloaded radio programs that consist of 4 mp3 files each. The files are named like so:
Show Name - Nov 28 2011 - Hour 1.mp3
Show Name - Nov 28 2011 - Hour 2.mp3
Show Name - Nov 28 2011 - Hour 3.mp3
Show Name - Nov 28 2011 - Hour 4.mp3
Show Name - Nov 29 2011 - Hour 1.mp3
Show Name - Nov 29 2011 - Hour 2.mp3
Show Name - Nov 29 2011 - Hour 3.mp3
Show Name - Nov 29 2011 - Hour 4.mp3
Show Name - Nov 30 2011 - Hour 1.mp3 and so on...
I have used the cat command to join the files with great success by moving the four files of the same date into a folder and using the wildcard:
cat *.mp3 > example.mp3
The files are all the same bitrate, sampling rate, etc.
What I would like to do is run a script that will look at the file name and combine hours 1-4 of each date and name the file accordingly. Just the show name, the date and drop the 'Hour 1'.
I looked around and found a number of scripts that can be used to move files around based on their names but I'm not adept enough at bash scripting to be able to understand the methods used and adapt them to my needs.
I'm using Ubuntu 14.04.
Many thanks in advance
You can use a bash for loop to find each distinct date name and then construct the expected mp3 names from that.
Because your files have spaces in their names and my solution uses globbing, you'll also have to edit your Internal Field Separator to ignore spaces for the duration of the script.
SAVEIFS=$IFS
IFS=$'\n\b'
for mdy in `/bin/ls *mp3 | cut -d' ' -f'4,5,6' | sort -u`; do
cat *${mdy}*.mp3 > "showName_${mdy}_full.mp3"
done
IFS=$SAVEIFS
This won't alert you if some hours are missing for some particular date. It'll just join together whatever's there for that date.
Note: The comment pointing out that cat probably won't work for these files is spot on. The resulting file will probably be corrupted. You probably want to use something like mencoder or ffmpeg instead. (Check out this thread.)

How can we get output in standard numeric format for expr command on solaris

I am expecting the output of following command as 03 not 3:
$expr 01 + 02
3
Do have any alternate way to get o/p like 01, 02...09
(Running command on Solaris8)

How to extract text by date range?

I am trying to extract text from a file according a given date range. The date range will be decided by the user but here I am just using a fixed range.
File content after using grep are as follows:
ronnie#ronnie:~$ zgrep added new.txt
Jul 02 21:03 : update: added Linkin Park/Living Things(2012)/02 - Linkin Park - In My Remains.mp3
Jul 02 21:03 : update: added Linkin Park/Living Things(2012)/03 - Linkin Park - Burn It Down.mp3
Jul 07 10:33 : update: added Linkin Park/Living Things(2012)/04 - Linkin Park - Lies Greed Misery.mp3
Jul 09 07:54 : update: added Linkin Park/Living Things(2012)/04 - Linkin Park - Lies Greed Misery.mp3
Now, lets suppose I want to extract the text between date Jul 07 and Jul 09. So I used the below command for that
zgrep added new.txt | sed '/"Jul 09"/,/"Jul 07"/p'
which gave me the following output
Jul 02 21:03 : update: added Linkin Park/Living Things(2012)/02 - Linkin Park - In My Remains.mp3
Jul 02 21:03 : update: added Linkin Park/Living Things(2012)/03 - Linkin Park - Burn It Down.mp3
Jul 07 10:33 : update: added Linkin Park/Living Things(2012)/04 - Linkin Park - Lies Greed Misery.mp3
Jul 09 07:54 : update: added Linkin Park/Living Things(2012)/04 - Linkin Park - Lies Greed Misery.mp3
So, as you can see it didn't considered the range i gave to sed.
My question is what should be the correct way to extract text according to a date range.
For ordered input,
command | sed -n '/^Jul 07/,/^Jul 09/p' inputFile
is sufficient.
You're pretty close, what you want is this:
zgrep added new.txt | sed -n -e '/Jul 09/,/Jul 07/p'
Changes were:
added -n, which means lines won't be printed unless you specifically say to with p
added -e, just for clarity
removed your double quotes around the strings. These are not needed because the expression is already enclosed in single quotes and the double quotes do not appear in your file.
Note that this, and your version, will only work if the lines are always ordered by date/time in the first place.

Resources