LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-03-2020, 02:17 AM   #16
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,621

Rep: Reputation: 732Reputation: 732Reputation: 732Reputation: 732Reputation: 732Reputation: 732Reputation: 732

Or print the offending lines
Code:
awk -F',' '$5~"/"; {print > ($5 ".txt") }' book2.csv
Note the semicolon after the condition; so the default action {print} applies.
Without the semicolon the following { } becomes the action.
 
1 members found this post helpful.
Old 04-05-2020, 07:43 PM   #17
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 1,850

Original Poster
Rep: Reputation: 337Reputation: 337Reputation: 337Reputation: 337
hi guys, your help and ideas is greatly appreciated.

just for update to anyone who might need it, the code below did not show any output redirection error despite of the data inside the csv whether it has slashes, colon and other special characters

Quote:
#!/bin/bash

input="/home/hrts/Documents/p/inputx.txt"
dfile="/home/hrts/Documents/Athena-Book2.csv"

while read -r line
do
#echo "$line"
#xx="awk -F',' '/$line/' $dfile > $line".txt""
#echo "$xx"

awk -F',' "/$line/" $dfile > $line".txt"

done < "$input"


#i-0a3f - is the pattern from input.txt
#awk -F',' '/i-0a3f/' book2.csv > i-0a3f.txt <--this would be the exact command that is executed after substituting the variable
The CSV has 330 unique patterns if i put everything the text file has no output, I have to break it into 30 chunks. Never tried by 50 or 100 but anyway script above works. Thanks everyone. Cheers!

Let's enjoy scripting and always be safe, stay at 127.0.0.1.
 
Old 04-06-2020, 02:07 AM   #18
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,621

Rep: Reputation: 732Reputation: 732Reputation: 732Reputation: 732Reputation: 732Reputation: 732Reputation: 732
In your previous script $line may not contain / characters, because it clashes with the awk syntax.
The shell substitutes $line so awk sees e.g /.../.../ - syntax error!

A better way is to pass $line to an awk variable
Code:
awk -v line="$line" -F',' '$0~line' $dfile > $line".txt"
Now awk sees line=.../... and that's okay.

Last edited by MadeInGermany; 04-06-2020 at 02:10 AM.
 
1 members found this post helpful.
Old 04-06-2020, 11:12 AM   #19
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 942

Rep: Reputation: 216Reputation: 216Reputation: 216
AFAIR in gawk you can redirect to as many file names as you want.

So for example

{
print $0 >> $5.txt
}

OK
 
Old 04-06-2020, 12:40 PM   #20
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 1,739

Rep: Reputation: Disabled
In gawk, yes, but default awk in Debian-based distributions is mawk.
 
  


Reply

Tags
awk


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
vi search multiple pattern and remove only exact matching pattern amateurscripter Linux - Newbie 4 05-07-2018 02:19 PM
Matching patterns or partial pattern matching yaplej Programming 6 12-16-2012 11:21 AM
[SOLVED] awk with pipe delimited file (specific column matching and multiple pattern matching) lolmon Programming 4 08-31-2011 01:17 PM
Help with pattern matching, sorting data with awk/gawk or perl placem Programming 2 09-11-2008 03:26 PM
AWK/SED Multiple pattern matching over multiple lines issue GigerMalmensteen Programming 15 12-03-2006 06:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration