Unix/Linux comma deliminated files [closed] - linux

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have comma deliminated file that basically has the structure of:
1, 2, 3, 4, 5 ,,,, 6, etc
I have to count the number of unique 6th columns. Pleasseee help
(btw this is an intro to unix/linux class so this should be able to be done with basic commands)

cut -d "," -f 6 myFile |sort |uniq -c |wc -l

Looking into my crystal ball, I see that your class is discussing awk. Try
awk -F, '!a[$6]++{c++} END{ print c }' input-file

Related

how can I remove some numbers at the end of line in a text file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
I have a text file which contains a series of same line except at the end.
eg
lesi-1-1500-1
lesi-1-1500-2
lesi-1-1500-3
how can I remove the last number? it goes upto 250
to change in the file itself
sed -i 's/[0-9]\+$//' /path/to/file
or
sed 's/[0-9]\+$//' /path/to/file > /path/to/output
see example
You can do it with Awk by breaking it into fields.
echo "lesi-1-1500-2" > foo.txt
echo "lesi-1-1500-3" >> foo.txt
cat foo.txt | awk -F '-' '{print $1 "-" $2 "-" $3 }'
The -F switch allows us to set the delimiter which is -. Then we just print the first three fields with - for formatting.

Extracting month from day using linux terminal [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am having a text file containing a list of date and time just like the sample below -
posted_at"
2012-06-09 11:48:31"
2012-08-09 12:40:02"
2012-04-09 13:10:00"
2012-03-09 13:40:00"
2012-10-09 14:30:01"
2012-12-09 15:30:00"
2012-11-09 16:20:00"
I want to extract the month from each line.
P.S - grep should not be used at any point of the code
Thanks in advance!
First select date pattern :
egrep '[0-9]{4}-[0-9]{2}-[0-9]{2} ' content_file
Second, extract the month :
awk -F '-' '{print $2}'
Third redirect to desired file :
>> desired_file
So mix of all this with | to final solution :
egrep '[0-9]{4}-[0-9]{2}-[0-9]{2} ' content_file| awk -F '-' '{print $2}'>> desired_file
VoilĂ 

How to create a Unix script to segregate data Line by Line? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have some data in a MyFile.CSV file like this:
id,name,country
100,tom cruise,USA
101,Johnny depp,USA
102,John,India
What will be the shell script to take the above file as input and segregate the data in 2 different files as per the country?
I tried using the FOR loop and then using 2 IFs inside it but I am unable to do so. How to do it using awk?
For LINE in MyFile.CSV
Do
If grep "USA" $LINE >0 Then
$LINE >> Out_USA.csv
Else
$LINE >> Out_India.csv
Done
You can try with this
grep -R "USA" /path/to/file >> Out_USA.csv
grep -R "India" /path/to/file >> Out_India.csv
Many ways to do:
One way:
$ for i in `awk -F"," '{if(NR>1)print $3}' MyFile.csv|uniq|sort`;
do
echo $i;
egrep "${i}|country" MyFile.csv > Out_${i}.csv;
done
This assumes that the country name would not clash with other columns.
If it does, then you can fine tune that by adding additional regex.
For example, it country will be the last field, then you can add $ to the grep

A bash loop to echo all possible ASCII characters [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I know how to print all letters
{a..z} and {A..Z} and {0..9}
But is there a way to print all possible ASCII Characters via a bash loop?
You don't need a loop
echo -e \\x{0..7}{{0..9},{A..F}}
It prints all chars from 0 to 127.
If it is okay to use awk:
awk 'BEGIN{for (i=32;i<127;i++) printf("%c", i)}'
Or using printf:
for((i=32;i<127;i++)) do printf "\x$(printf %x $i)"; done
use this:
for ((i=32;i<127;i++)) do printf "\\$(printf %03o "$i")"; done;printf "\n"

Linux Compare two text files [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have two text file like below:
File1.txt
A|234-211
B|234-244
C|234-351
D|999-876
E|456-411
F|567-211
File2.txt
234-244
999-876
567-211
And I want to compare both files and get containing values like below:
Dequired output
B|234-244
D|999-876
F|567-211
$ grep -F -f file2.txt file1.txt
B|234-244
D|999-876
F|567-211
The -F makes grep search for fixed strings (not patterns). Both -F and -f are POSIX options to grep.
Note that this assumes your file2.txt does not contain short strings like 11 which could lead to false positives.
Try:
grep -f File2.txt File1.txt

Resources