Deleting Junks at the end of each line in a file

Is there any way to delete the Junk Characters(Invalid Characters like ^,',",),(,&,# etc.,) at the end of each record in a file?

I want to do this using a single line script.

Thanks to all in advance!!!

Define "junk"

Define "record"

Define "single line script", is back-slash allowed? What about the interpretor indicator in the first line? Does it have to fit in 131, 79 or 65 characters?

Are there any other nefarious restrictions, like can't use sed or awk?

Single line Script - You can use any commands but i dont want for loop,if loop,while loop etc.,I want a simple script.

Record - Am calling Each line in a file as record

Junk - As I said unwanted characters including backslash

Ex:
"401","1G1AL55F377159935 ",30482,"MD","3","09/17/2007",9000 -Valid record
"401","1G1AL55F377159935 ",30482,"MD","3","09/17/2007",9000[ - invalid
"401","1G1AL55F377159935 ",30482,"MD","3","09/17/2007",9000; - invalid
"401","1G1ZT58N47F140841 ",29098,"MD","3","09/17/2007",10600'" - invalid
"401","1G1ZT68N57F138781 ",27403,"GA","1","09/17/2007",10100' - invalid
"401","1G4HD57227U217222 ",7163,"MI","3","09/17/2007",21600: - invalid

You can certainly use awk or sed!!
Thanks!:slight_smile:

We don't know what you don't want, do you mean all non numeric characters?

Is it always a single character at the end?

As I'm not into awk, here is something else...

#include <stdio.h>
#include <string.h>

static int is_junk(c)
int c;
{
	if ((c >= '0')&&(c <='9')) return 0;
	return 1;
}

int main()
{
char buf[4096];

	while (fgets(buf,sizeof(buf),stdin))
	{
		int i=strlen(buf);
		while (i--)
		{
			if ((buf=='\n')||is_junk(buf))
			{
				buf=0;
			}
			else
			{
				break;
			}
		}
		printf("%s\n",buf);
	}

	return 0;
}

try this

awk 'BEGIN{OFS=FS=","}{$NF=$NF+0}1' file