Showing posts with label free downloads for Unix FAQs. Show all posts
Showing posts with label free downloads for Unix FAQs. Show all posts

Finding Numeric Word at eof Field

The file has the numeric value toward the end of the first field,
" La la la, bla bla bla 123 bla bla ", "ksdjf"
" La , Bla 123 la la blala bla", "ksdjkjf"

I want to check the -3 word of the first field, if it is numeric, then add "," before the -3 word to delimit a new field. If not check the -4 word , if it is numberic then add "," before it. This will isolate the numeric word and the following text in field 2. It needs to work from the end of the field.

Sed? Gawk? Awk? Grep?

A possible solution with 'awk'
-----------
awk '
{
# Get Field 1

if (match($0, /^"[^"]*",/) == 0) {
print $0;
next;
}
field1 = substr($0,1,RLENGTH-1);

# Search for number in word 3 or 4 starting from the end of field1

if (match(field1,/[0-9]+ +[^ ]+ +[^ ]+ *"$/) == 0) {
if (match(field1,/[0-9]+ +[^ ]+ +[^ ]+ +[^ ]+ *"$/) == 0) {
print $0;
next;
}
}

# Insert "," before number

print substr($0,1,RSTART-1) "\",\"" substr($0,RSTART,length($0)-RSTART+1);
}
' input_file
-----------

If your version of awk supports "interval expression", you can rewrite the two last if statements :

if (match(field1,/[0-9]+( +[^ ]+){2} *"$/) == 0) {
if (match(field1,/[0-9]+( +[^ ]+){3} *"$/) == 0) {

With the following input data :

" La la la, bla bla bla 123 bla bla ", "ksdjf"
" La , Bla 123 lala blala bla", "ksdjkjf"
" La , Bla 123 la la blala bla", "ksdjkjf"

The result is :

" La la la, bla bla bla ","123 bla bla ", "ksdjf"
" La , Bla ","123 lala blala bla", "ksdjkjf"
" La , Bla 123 la la blala bla", "ksdjkjf"

Changing Upper and Lower Case

What is the command for changing the following:
KREMS|XXXX YYYY|HHHH|
YYYY UUUUU |YYYYYYYYY|IIIIII

Need to change it to:
Krems|Xxxxx Yxxx|Hhhh|
Yyyy Uuuuu |Yyyyyyyyy|Iiiiii

I have a large file with proper names that need to be converted to upper and lower case.

---

The following solution leaves each character as is that follows either start of line or space or bar character, and downshifts all the rest.
I leave the first character as is (rather than upshift) because you may want something like John deHavilland. But if you do want to upshift all first characters, then remove the #.

awk '{\
newline=""
cap=1
for (i=1;i<=length;i++) {x=substr($0,i,1) if (x=="|" || x==" ") cap=1 else if (cap==1) {#x=toupper(x) cap=0} else x=tolower(x) newline=newline x} print newline }' largefile

---

works fine except for if I have the following
18.yyyyyy-kkkkkk|

need it to be
18.Yyyyyy-Kkkkkk|

Any ideas on how to include these as well

---

Change the if-statement to:
if (x=="|" || x==" " || x=="." || x=="-")

and since you want to upshift (not just downshift), then take out the #.