How to read data from tab delimited file after a specific position?

Hi Experts,

I have a tab deliminated file as below

myfile.txt
Local Group Memberships *Administrators *Guests

I need data in below format starting from 4th position.
myfile1.txt

Administrators Guests

the above one is just an example and there could any number of columns only starting 1-3rd position is constant.basically i need all data except starting 3 position.

pls suggest

Thanks,
Litu

Try

sed -r 's#\*##g;s#^([^ ]* ){3}##' file

it results as below
Local Group MembershipsAdministrators Guests

I need
Administrators Guests

Hello Litu,

If your requirement is to get results except first 3 columns then following may help you in same.

awk '{$1=$2=$3=""} 1'  Input_file

Thanks,
R. Singh

Could you show an actual, representative sample of the file. I doubt it is strictly TAB delimited ("local group memberships" seems like a single field name?)

For a strictly TAB delimited file, you could try this:

awk '{$1=$2=$3=x; sub(/^\t\t\t/,x)}1' FS='\t\\**' OFS='\t' file

thanks R Singh

it results as below
*Administrators *Guests

with a tab character at the beginning.

pls find attached

mytxt.txt- actual file
mytxt1.txt- expected file(even comma separated format would be fine as output file)

Thanks

These files are DOS-type files with Carriage Returns and they contain no TABS.

--
Try:

awk '{$1=$2=$3=x; gsub(/\*/,x); $1=$1}1'  file

If it's really a TAB delimited file, then following cut command should work

cut -f4- myfile

but it will produce an empty line if a line has less than 4 fields.
---
This awk command will ignore lines with less than 4 fields:

awk 'NF>=4 {for (i=4;i<=NF;i++) printf $i" "; print ""}' myfile

Scrutinizer thanks.

attached the output file

currently it is in below format.
Administrators Guests
None

It would be really gr8 if all the data in a single line with either tab or comma separated format.(with current format, I have a workaround but it may not be gr8 for long run)

Administrators,Guests,None
Administrators Guests None

Awright. Try:

awk 'END{printf RS} {$1=$2=$3=x; gsub(/\*|\r/,x); $1=$1}1' OFS='\t' ORS='\t'  file

Scrutinizer,
command execution failed with below error

\r not recognized as an internal or external command,operable program or batch file.

Right... "batch file" ??? You are running awk on a Windows OS then...

Try putting this in a file, for example tst.awk

BEGIN {
  OFS=ORS="\t"
}

{
  $1=$2=$3=x
  gsub(/\*|\r/,x)
}

END {
  printf RS
}

And run it like this:

awk -f tst.awk file

---
It will output the file in UNIX format. Is that what you want?

Otherwise, replace

printf RS

with

printf "\r\n"

yes it is a windows OS but earlier command was also awk that you have suggested and it was executed successfully

You should always explicitly mention you are running on Windows, because it behaves quite differently. These are Unix and Linux forums...

I am creating a script and this will execute against many Win OS. the last solution may not work for me becoz I need to have a copy of tst.awk in all the windows OS against which we will execute the script.

I dont want to trouble you anymore.

currently data is in below format.

Administrator Guest

I need to read each column data and loop through for some other logic to work. at a time I need to read one column value and pass it as an argument to the next command n the loop. pls suggest something here.

Alternatively we can format the data into below format. this will be helpful for me to iterate the loop.

Administrator
Guests

---------- Post updated at 06:11 PM ---------- Previous update was at 06:08 PM ----------

My bad, will take care in future. I should have mentioned that it is for Windows.