LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 08-05-2015, 10:15 AM   #1
unixmon
LQ Newbie
 
Registered: Aug 2015
Posts: 9

Rep: Reputation: Disabled
How to use AWK to trim file names


I want to remove the .1.gz component of the following file names in a column listing using AWK

autoconf.1.gz
autoheader.1.gz
autom4te.1.gz
automake-1.11.1.gz
automake.1.gz
autopoint.1.gz

I have found a number of options using Google but they are either too complex or they don't apply to this at all. I know that there is a simple AWK statement that will do this, I am just having difficulty figuring it out.
 
Old 08-05-2015, 10:16 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,849

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
would be nice to see what have you really tried...
 
Old 08-05-2015, 10:34 AM   #3
unixmon
LQ Newbie
 
Registered: Aug 2015
Posts: 9

Original Poster
Rep: Reputation: Disabled
First, I captured the names of the Linux man page 1-9 files and >> to a file that had 9 columns. Then I used the following command: awk '2>0{print $9}' ./LinuxCmds_v1e1.txt >> test to capture just the file, names which included the .1.gz component that I wish to remove. I think that AWK might be the right choice for this but I might have to use VI to do so. I can cheat by SCP'ing the file to my laptop, creating a word document and using Find/Replace or perhaps doing something like that in Excel, BUT I want to find a Linux solution. Any suggestions?
 
Old 08-05-2015, 12:27 PM   #4
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
Not sure what you are trying to do. Here are some examples.

Code:
echo autoconf.1.gz | cut -d '.' -f1

echo autoconf.1.gz | awk -F '.' '{print $1}'

echo autoconf.1.gz | sed 's/.1.gz//'
Code:
#! /usr/bin/env bash

list="
autoconf.1.gz
autoheader.1.gz
autom4te.1.gz
automake-1.11.1.gz
automake.1.gz
autopoint.1.gz
"

#Example 1
for i in "$list"; do
  echo "$i" | cut -d '.' -f1
done

#Example 2
for i in $list; do
  echo ${i%%.*}
done

#Example 3
awk -F '.' '{print $1}' <<< "$list"
 
Old 08-05-2015, 12:47 PM   #5
unixmon
LQ Newbie
 
Registered: Aug 2015
Posts: 9

Original Poster
Rep: Reputation: Disabled
Hi All,

Ended up using vi to do this. vi'd the file that contained

autoconf.1.gz
autoheader.1.gz
autom4te.1.gz
automake-1.11.1.gz
automake.1.gz
autopoint.1.gz

and did the following:

:s/.1.gz/ /

to get

autoconf
autoheader
autom4te
automake-1.11
automake
autopoint

Thanks for responding to my query. I am now an official member of linuxquestions.org!

Unixmon
 
Old 08-05-2015, 12:49 PM   #6
unixmon
LQ Newbie
 
Registered: Aug 2015
Posts: 9

Original Poster
Rep: Reputation: Disabled
Correction

Actually :s/.1.gz/ /

needed the % to do all of the lines in the file

Unixmon
 
Old 08-05-2015, 12:50 PM   #7
unixmon
LQ Newbie
 
Registered: Aug 2015
Posts: 9

Original Poster
Rep: Reputation: Disabled
Nuts!

Actually :s/.1.gz/ /

Unixmon
 
Old 08-05-2015, 12:52 PM   #8
unixmon
LQ Newbie
 
Registered: Aug 2015
Posts: 9

Original Poster
Rep: Reputation: Disabled
:s/.1.gz/ /
 
Old 08-05-2015, 12:53 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
You may wish to have a look at sed and using your same solution
 
Old 08-05-2015, 12:55 PM   #10
unixmon
LQ Newbie
 
Registered: Aug 2015
Posts: 9

Original Poster
Rep: Reputation: Disabled
Wow, I type a percent symbol after the colon in the vi edit string into this Quick Reply text box and, when the message posts, the percent symbol is gone. Could someone else give this a try to see if they get the same result?
 
Old 08-05-2015, 12:59 PM   #11
unixmon
LQ Newbie
 
Registered: Aug 2015
Posts: 9

Original Poster
Rep: Reputation: Disabled
Can't figure out how to Mark as Solved :|

"Arghhh, it was me first day with the hook."
 
Old 08-05-2015, 01:01 PM   #12
unixmon
LQ Newbie
 
Registered: Aug 2015
Posts: 9

Original Poster
Rep: Reputation: Disabled
There it was, right at the top... Doh!
 
  


Reply



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
How to print the file names using AWK Newman1 Programming 5 07-26-2015 06:04 AM
[SOLVED] Trim operation using awk luvshines Programming 6 10-11-2011 03:17 PM
[SOLVED] Script: Trim all file names recursively Flukester Linux - Server 9 10-28-2009 06:37 AM
Trim first 10 lines out of a file hattori.hanzo Linux - Newbie 7 11-12-2008 08:40 AM
How To Trim A File fpfernando Programming 11 01-05-2006 08:04 AM

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

All times are GMT -5. The time now is 08:40 AM.

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