0

I need to delete "" in file

"CITFFUSKD-E0"

I have tried sed 's/\"//.

Result is:

CITFFUSKD-E0"

How I can delete both ?

Also I need to delete everything behind first word but input can be this one:

"CITFFUSKD-E0"
"CITFFUSKD_E0"
"CITFFUSKD E0"

Result I want it:

CITFFUSKD
Cyrus
  • 84,225
  • 14
  • 89
  • 153
sundrys1
  • 305
  • 1
  • 8

7 Answers7

1

You may use

sed 's/"//g' file | sed 's/[^[:alnum:]].*//' > newfile

Or, contract the two sed commands into one sed call as @Wiimm suggests:

sed 's/"//g;s/[^[:alnum:]].*//' file > newfile

If you want to replace inline, see sed edit file in place.

Explanation:

  • sed 's/"//g' file - removes all " chars from the file
  • sed 's/[^[:alnum:]].*//' > newfile - also removes all chars from a line starting from the first non-alphanumeric char and saves the result into a newfile.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • shorter version of `sed 's/"//g' file | sed 's/[^[:alnum:]].*//' > newfile`: `sed 's/"//g; s/[^[:alnum:]].*//' file > newfile` – Wiimm May 30 '19 at 10:15
  • @Wiimm Right, I always focus on the immediate problem when it comes to sed and forget about possible enhancements like this. – Wiktor Stribiżew May 30 '19 at 10:50
0

Could you please try following.

awk 'match($0,/[a-zA-Z]+[^a-zA-Z]*/){val=substr($0,RSTART,RLENGTH);gsub(/[^a-zA-Z]+/,"",val);print val}' Input_file
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
0

delete everything behind first word

sed 's/^"\([[:alpha:]]*\)[^[:alpha:]]*.*/\1/'

Match the first ". Then match a sequence of alphabetic characters. Match until you find non-alphabetic character ^[:alpha:]. Then match the rest. Substitute it all for \1 - it is a backreference for the part inside \( ... \), ie. the first word.

I need delete two “ ” with sed command

Remove all possible ":

sed 's/"//g'

Extract the string between ":

sed 's/"\([^"]*\)"/\1/'

Remove everything except alphanumeric characters (numbers + a-z + a-Z, ie. [0-9a-zA-z]):

sed 's/[^[:alnum:]]//g'
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
0

This should do all in one go, remove the ", print the first part:

awk -F\" '{split($2,a,"-| |_");print a[1]}' file
CITFFUSKD
CITFFUSKD
CITFFUSKD
Jotne
  • 40,548
  • 12
  • 51
  • 55
0

When you have 1 line, you can use

grep -Eo "(\w)*" file | head -1

For normal files (starting with a double quote on each line) , try this

tr -c [^[:alnum:]] '"' < file | cut -d'"' -f2
Walter A
  • 19,067
  • 2
  • 23
  • 43
0

Many legitimate ways to solve this.

I favor using what you know about your data to simplify solutions -- this is usually an option. If everything in your file follows the same pattern, you can simply extract the first set of capitalized letters encountered:

sed 's/"\([A-Z]\+\).*$/\1/' file
Kyle Banerjee
  • 2,554
  • 4
  • 22
  • 30
0

awk '{gsub(/^.|....$/,"")}NR==1' file

CITFFUSKD

Claes Wikner
  • 1,457
  • 1
  • 9
  • 8