15762

output csv with lines that contains only one column

Question:

with input csv file

sid,storeNo,latitude,longitude 2,1,-28.03720000,153.42921670 9

I wish to output only the lines with one column, in this example it's line 3. how can this be done in bash shell script?

Answer1:

<strong>Using awk</strong>

The following awk would be usfull

$ awk -F, 'NF==1' inputFile 9

<strong>What it does?</strong>

<ul><li>

-F, sets the field separator as ,

</li> <li>

NF==1 matches lines with NF, number of fields as 1. No action is provided hence default action, printing the entire record is taken. it is similar to NF==1{print $0}

</li> <li>

inputFile input csv file to the awk script

</li> </ul>

<strong>Using grep</strong>

The same function can also be done using grep

$ grep -v ',' inputFile 9 <ul><li>

-v option prints lines that do not match the pattern

</li> <li>

, along with -v greps matches lines that do not contain , field separator

</li> </ul>

<strong>Using sed</strong>

$ sed -n '/^[^,]*$/p' inputFile 9

<strong>what it does?</strong>

<ul><li>

-n suppresses normal printing of pattern space

</li> <li>

'/^[^,]*$/ selects lines that match the pattern, lines without any ,

<ul><li>

^ anchors the regex at the start of the string

</li> <li>

[^,]* matches anything other than ,

</li> <li>

$ anchors string at the end of string

</li> </ul></li> <li>

p action p makes sed to print the current pattern space, that is pattern space matching the input

</li> </ul>

Answer2:

try this bash script

#!/bin/bash while read -r line do IFS="," set -- $line case ${#} in 1) echo $line;; *) continue;; esac done < file

Recommend

  • Bash replace in CSV multiple columns
  • awk/sed/grep extract part of lines with specific pattern
  • Java - How to convert this string to date?
  • Storing command output lines into array based on new line character
  • Add vertical separator and labels to R barplot
  • use grep and awk to transfer data from .srt to .csv/xls
  • Gruntfile.js - Throwing error 'Recursive process.nextTick detected\"
  • Unable to resolve Static method?
  • c++ regex_replace not doing intended substitution
  • How to render a pixel array most efficiently to a window in c++?
  • abstracting over a collection
  • In matplotlib, how do you change the fontsize of a single figure?
  • Find group of records that match multiple values
  • Calculate time difference in hh:mm:ss with simple javascript/jquery
  • Hide HTML elements without javascript, only CSS
  • TFS 2015 - Waiting for an agent to be requested
  • Redirect STDERR in OPEN pipe comand. Perl Linux
  • Remove changes from one element when event occurs on another element?
  • RxJava debounce by arbitrary value
  • Android Google Maps API v2 start navigation
  • Entity Framework Code First TPC Inheritance Self-Referencing Child Class
  • how to avoid repetitive constructor in children
  • How to have background script and something similar to a default popup?
  • Spark fat jar to run multiple versions on YARN
  • Is there a javascript serializer for JSON.Net?
  • How can I send an e-mail from a vbs script
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Retrieving value from sql ExecuteScalar()
  • Regex thinks I'm nesting, but I'm not
  • Can Jackson SerializationFeature be overridden per field or class?
  • Where to put my custom functions in Wordpress?
  • ActionScript 2 vs ActionScript 3 performance
  • json Serialization in asp
  • Buffer size for converting unsigned long to string
  • KeystoneJS: Relationships in Admin UI not updating
  • Hits per day in Google Big Query
  • how does django model after text[] in postgresql [duplicate]
  • Checking variable from a different class in C#
  • Django query for large number of relationships
  • Binding checkboxes to object values in AngularJs