LinuxQuestions.org
Help answer threads with 0 replies.
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 09-06-2022, 09:49 PM   #16
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,763

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931

You never answered the questions on spacing, is it fixed, how many, is it variable etc.
Change OFS as desired. $nbr is the number of fields to ouput i.e x=6 will output 1-6.

Code:
awk -v x=$nbr 'BEGIN {OFS="  "} {NF =  x} {print $0 }' input.file
I don't know of an easy way to drop one field with awk. But then again this is a new requirement? cut would be better but does not handle multiple field separator.

Last edited by michaelk; 09-06-2022 at 09:59 PM.
 
Old 09-06-2022, 11:57 PM   #17
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,146

Rep: Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124
Quote:
Originally Posted by michaelk View Post
$nbr is the number of fields to ouput
Not from my example. The OP indicates the number of fields to be deleted from the end is known, and that's what I coded for. Likewise grail.

Substringing would work but looks to be messy with awk in this case - sed is likely more robust here. Maybe just use a bash case for the various $nbr values.
 
Old 09-07-2022, 01:29 AM   #18
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,817

Rep: Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211
Yet untested:
Code:
nbr=3
sed "s/\([[:space:]]*[^[:space:]]\{1,\}\)\{$nbr\}[[:space:]]*$//" inputfile
 
Old 09-07-2022, 04:47 AM   #19
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Thnaks syg00 for the follow up (thought I had mis-read)

I think the question becomes, is the whitespace important and if so, what is it going into that needs it kept that way??

Side note :- I see a number of people have mentioned a cut solution. I for one would be curious to see this in action with different sized spacing in the input?

Last edited by grail; 09-07-2022 at 04:50 AM.
 
Old 09-08-2022, 12:46 AM   #20
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,377

Rep: Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757
Quote:
Side note :- I see a number of people have mentioned a cut solution. I for one would be curious to see this in action with different sized spacing in the input?
I take your point, cut is not the tool with different sized spacing (which was not apparent when I initially posted), but for giggles.
Code:
sed 's/[[:space:]]\([^[:space:]]\)/ \t\1/g' inputfile | rev | cut -f 1-3 --complement | rev | tr -d "\t"
 
  


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
Get first day of last month and last day of last month in bash xowl Linux - Software 18 02-09-2017 09:49 AM
drag-drop copying and cp command both hang at last few KB when copying to flash drive slacker_ Linux - Newbie 1 09-05-2013 07:17 AM
dividing a string into substrings with three elements in C++ lindaonline15 Programming 1 06-23-2011 10:49 AM
conting repeated substrings:SED perl anirudh Programming 1 10-29-2004 01:01 PM
Substrings in C marri Programming 4 08-24-2004 04:52 AM

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

All times are GMT -5. The time now is 01:29 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